我正在尝试创建联系表单以发送电子邮件(from
和to
将来自用户界面):
try {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("fromadd");
mail.To.Add("toadd");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username","password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
这仅适用于Gmail - 但是,我想让它适用于任何电子邮件提供商 - 我将如何解决这个问题?
答案 0 :(得分:2)
您应该在web.config中配置SmtpClient:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network
host="localhost"
port="25"
defaultCredentials="true"
/>
</smtp>
</mailSettings>
</system.net>
</configuration>
然后在你的代码中你可以这样做:
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("fromadd");
mail.To.Add("toadd");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
答案 1 :(得分:1)
您可以在web.config中设置SmtpClient
配置。这将使其变得灵活。
答案 2 :(得分:1)
请勿使用硬编码参数连接到smtp-server。
请改用webconfig。你的程序将更“通用”。当你想通过另一个smtp-server发送时,只需更改配置
答案 3 :(得分:1)
您也可以尝试:
MailMessage msgObj = new MailMessage();
msgObj.To = "example@example.com";
msgObj.From = "Mike";
msgObj.Bcc = "example@example.com";
msgObj.Subject = "Test Message";
msgObj.Body = "Hello World!";
SmtpMail.SmtpServer = "Your Server";
SmtpMail.Send("FromEmail", "ToEmail", "Subject", "Query");
SmtpMail.Send(msgObj);
答案 4 :(得分:0)
对我来说,似乎唯一可能阻止您使用任何电子邮件服务器发送电子邮件的事实是某些邮件服务器需要身份验证(或可能是其他端口号)。
这里有一些基本代码可以让你指向正确的方向
public class SendMail
{
public SendMail(string SMTPServer, string fromEmail)
{
this.SMTPServer = SMTPServer;
this.FromEmail = fromEmail;
}
public SendMail(string SMTPServer, string fromEmail, string Username, string Password) : this(SMTPServer, fromEmail)
{
this.Username = Username;
this.Password = Password;
}
public string SMTPServer { get; set; }
public string FromEmail { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public void Send(string toEmail, string subject, string data)
{
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(toEmail);
MailAddress mailAddress = new MailAddress(this.FromEmail);
mailMsg.From = mailAddress;
mailMsg.Subject = subject;
mailMsg.Body = data;
mailMsg.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient(this.SMTPServer, 25);
if (this.Username.Length > 0 && this.Password.Length > 0)
{
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(this.Username, this.Password);
smtpClient.Credentials = credentials;
}
smtpClient.Send(mailMsg);
}
}
答案 5 :(得分:0)
我正在尝试此代码。
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("best@technosys.com");
mail.To.Add("best@technosys.com");
mail.Subject = "Accept Request";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("best@technosys.com", " password");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.Send(mail);
}
catch (Exception ex)
{
ViewData.ModelState.AddModelError("_FORM", ex.ToString());
}