5

我正在尝试创建联系表单以发送电子邮件(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 - 但是,我想让它适用于任何电子邮件提供商 - 我将如何解决这个问题?

4

6 回答 6

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());
    }
于 2012-10-30T15:03:28.270 回答
1

您可以在 web.config 中设置SmtpClient配置。这将使其灵活。

http://blog.dotnetclr.com/archive/2009/08/18/511.aspx

http://msdn.microsoft.com/en-us/library/w355a94k.aspx

于 2012-10-30T14:58:41.720 回答
1

You can also try:

    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);
于 2013-10-17T15:22:44.620 回答
1

不要使用硬编码参数连接到 smtp 服务器。

请改用 webconfig。您的程序将更加“通用”。当您想通过另一个 smtp-server 发送时,只需更改配置

于 2012-10-30T14:59:34.063 回答
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);
    }

}
于 2012-10-31T06:49:00.763 回答
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());
}
于 2012-11-05T06:48:38.637 回答