从asp.net应用程序发送电子邮件

我使用c#配置发送电子邮件的所有设置但是当我执行时我得到以下错误请求的地址在其上下文中无效74.125.53.109:25

我的代码是

MailMessage mail = new MailMessage(); mail.To.Add("to@gmail.com"); mail.From = new MailAddress("from@gmail.com"); mail.Subject = "Test Email"; string Body = "Welcome to CodeDigest.Com!!"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = ConfigurationManager.AppSettings["SMTP"]; smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]); smtp.EnableSsl = true; smtp.Send(mail); 

Web.Config中

      

通过Gmail在.NET中发送电子邮件此链接有一个完整的代码,用于通过我的电脑中的gmail发送电子邮件及其正常工作的电子邮件 。

我想我会在这里发布post的综合努力:

将其添加到配置文件中,更改电子邮件/用户名/密码。 您可能必须根据Brian Rogers发布的内容更改端口。

         

在代码中使用它

  MailMessage mail = new MailMessage(); mail.To.Add("to@gmail.com"); mail.From = new MailAddress("from@gmail.com"); mail.Subject = "Test Email"; string Body = "Welcome to CodeDigest.Com!!"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Send(mail); 

端口25是默认端口,但不是通过SSL发送电子邮件的正确端口。 由于您使用的是SSL,因此需要设置smtp.Port = 465 ,根据Google主题的帮助页面: http : //support.google.com/mail/bin/answer.py? smtp.Port = 465

我认为该地址无效,因为它在SSL连接的上下文中以端口25为目标。

我已经这样做了,我已经在Gmail中测试了它。

StackOverflow – 发送带附件的电子邮件

Gmail端口= 465
使用SSL = true

您无法在IP之后将端口25指定为:25

它将默认为端口25,因此您不需要它。 如果要更改端口,请使用以下命令:

 mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpserverport", "portnumber" ); 

这是一个适用于发送邮件的function,我检查了它并且它正在工作。

 private static bool testsendemail(MailMessage message) { try { MailMessage message1 = new MailMessage(); SmtpClient smtpClient = new SmtpClient(); MailAddress fromAddress = new MailAddress("FromMail@Test.com"); message1.From = fromAddress; message1.To.Add("ToMail@Test1.com"); message1.Subject = "This is Test mail"; message1.IsBodyHtml = true; message1.Body ="You can write your body here" + message; // We use yahoo as our smtp client smtpClient.Host = "smtp.mail.yahoo.com"; smtpClient.Port = 587; smtpClient.EnableSsl = false; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new System.Net.NetworkCredential( "SenderMail@yahoo.com", "YourPassword" ); smtpClient.Send(message1); } catch { return false; } return true; } 
 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.Net.Mail; public partial class SendMail : System.Web.UI.Page { protected void btnSend_Click(object sender, EventArgs e) { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress("xxx@yourdomain.com"); msg.To.Add(txtTo.Text); //Text Box for To Address msg.Subject = txtSubject.Text; //Text Box for subject msg.IsBodyHtml = true; msg.Body = txtBody.Text; //Text Box for body msg.Priority = MailPriority.High; System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient( "relay-hosting.secureserver.net", 25 ); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential( "xxx@yourdomain.com", "yourpassword" ); client.Host = "relay-hosting.secureserver.net"; client.EnableSsl = false; object userstate = msg; client.Send(msg); } }