本文介绍了.NET发送电子邮件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试通过我的应用程序发送电子邮件,但它给了我一个例外。电子邮件发送失败内部异常无法连接到远程服务器。 这个问题来自我的域以及当我尝试使用gmail smtp设置时。 string ReplyTo = [email protected]; SmtpClient client = new SmtpClient( localhost ); MailAddress 来自 = new MailAddress( [email protected], Hello ); MailAddress to = new MailAddress( To @ mydomain.com); MailMessage message = new MailMessage( from ,to); client.Credentials = new System.Net.NetworkCredential( MYID, PASSWORD); message.Body = Body; message.Subject = Subject; StringBuilder str = new StringBuilder(); str.AppendLine( 你好这是一个测试邮件); StringBuilder str = new StringBuilder(); str.AppendLine( BEGIN:VCALENDAR); str.AppendLine( PRODID: - // HR21); str.AppendLine( VERSION:2.0); str.AppendLine( METHOD:PUBLISH); str.AppendLine( BEGIN:VEVENT); str.AppendLine( string .Format( DTSTART:{0:yyyyMMddTHHmmssZ},FromDate.ToUniversalTime())); str.AppendLine( string .Format( DTSTAMP:{0:yyyyMMddTHHmmssZ},DateTime.Now.ToUniversalTime())); str.AppendLine( string .Format( DTEND:{0:yyyyMMddTHHmmssZ},ToDate.ToUniversalTime())); str.AppendLine( LOCATION:None); str.AppendLine( string .Format( UID:{0},Guid.NewGuid())); str.AppendLine( string .Format( DESCRIPTION:{0},message.Body)); str.AppendLine( string .Format( X-ALT-DESC; FMTTYPE = text / html:{0},message.Body)); str.AppendLine( string .Format( 摘要:{0},message.Subject)); str.AppendLine( string .Format( ORGANIZER:MAILTO:{0},message.From.Address)); str.AppendLine( string .Format( ATTENDEE; CN = \{0} \; PARTSTAT = ACCEPTED:mailto:{1},message.To [ 0 ].DisplayName,message.To [ 0 ]。地址)); str.AppendLine( BEGIN:VALARM); str.AppendLine( TRIGGER:-PT15M); str.AppendLine( ACTION:DISPLAY); str.AppendLine( DESCRIPTION:Reminder); str.AppendLine( END:VALARM); str.AppendLine( END:VEVENT); str.AppendLine( END:VCALENDAR); System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType( text / calendar); ct.Parameters.Add( method, PUBLISH); AlternateView avCal = AlternateView.CreateAlternateViewFromString( abc,ct); message.AlternateViews.Add(avCal); client.Send(message); 解决方案 您不能发送电子邮件作为localhost作为您的SMTP客户端。这必须是合法的才能发送电子邮件。 除了 SmtpClient客户端=新的SmtpClient(localhost之外,它与您的代码中的任何内容无关。 ); 。它假定您在本地主机上运行SMTP服务器。我怀疑你这样做。此参数应该是运行某些SMTP服务的主机的主机名或IP地址。 -SA System.Net.Mail.MailMessage meg = new System.Net.Mail.MailMessage(); System.Net.Mail.SmtpClient mailsender = new System.Net.Mail.SmtpClient(); 尝试 { string su = 您的消息 string attachfilepath = ; // apppath获取文件路径 if (File.Exists(apppath + \\\\ErrorLogs.txt)== true ){ attachfilepath = apppath + \\ErrorLogs.txt; } else if (File.Exists(apppath + \\ErrorLogs.txt)== true ){ attachfilepath = apppath + \\ErrorLogs.txt; } 附件atm = new 附件(attachfilepath); meg.From = new MailAddress( From Address, SendeName); // 发件人的电子邮件地址和显示名称(可选) meg.To.Add( [email protected]); // 要解决 meg.CC.Add( [email protected]); // Cc meg.Bcc.Add( [email protected]); // Bcc meg.Subject = 这是文本邮件; // 电子邮件主题 meg.Body = su; // 电子邮件正文,即电子邮件的内容 meg.IsBodyHtml = true ; meg.Attachments.Add(atm); // 附件 配置configurationFile = WebConfigurationManager.OpenWebConfiguration( HttpContext.Current.Request.ApplicationPath + / web.config); MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup( system.net/mailSettings); if (mailSettings!= null ){ mailsender。 Host = mailSettings.Smtp.Network.Host; mailsender.Port = mailSettings.Smtp.Network.Port; if (Convert.ToBoolean(mailSettings.Smtp.Network.DefaultCredentials)){ mailsender.UseDefaultCredentials = 真; } if (Convert.ToBoolean(mailSettings.Smtp.Network.EnableSsl)){ mailsender.EnableSsl = 真; } mailsender.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName,mailSettings.Smtp.Network.Password) ; } mailsender.Send(meg); } catch (Exception ex){ ShowMessage(ex.Message); } 最后 { ShowMessage( Mail成功发送); } 最佳编码尝试 给出电子邮件地址和密码 in web.config file 此处代码 web.config < ; pre lang = xml > < system.net > < mailSettings > < smtp > < network defaultCredentials = true host = smtp .gmail.com 密码 = 密码 port = 587 userName = emailaddress enableSsl = true / > < / smtp > < / mailSettings > ; < 设置 > < httpWebRequest useUnsafeHeaderParsing = true / > < / settings > < / system.net > < / pre > 尝试将端口465更改为587 hi, i am trying to send an email through my application, but it gives me an exception. Email sending failure. Inner exception "Unable to connect to the remote server".this problem comes for my domain as well as when i try to use gmail smtp settings. string ReplyTo = "[email protected]"; SmtpClient client = new SmtpClient("localhost"); MailAddress from = new MailAddress("[email protected]", "Hello"); MailAddress to = new MailAddress("[email protected]"); MailMessage message = new MailMessage(from, to); client.Credentials = new System.Net.NetworkCredential("MYID", "PASSWORD"); message.Body = Body; message.Subject = Subject; StringBuilder str = new StringBuilder(); str.AppendLine("Hello this is a test email");StringBuilder str = new StringBuilder(); str.AppendLine("BEGIN:VCALENDAR"); str.AppendLine("PRODID:-//HR21"); str.AppendLine("VERSION:2.0"); str.AppendLine("METHOD:PUBLISH"); str.AppendLine("BEGIN:VEVENT"); str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", FromDate.ToUniversalTime())); str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime())); str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", ToDate.ToUniversalTime())); str.AppendLine("LOCATION: None"); str.AppendLine(string.Format("UID:{0}", Guid.NewGuid())); str.AppendLine(string.Format("DESCRIPTION:{0}", message.Body)); str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", message.Body)); str.AppendLine(string.Format("SUMMARY:{0}", message.Subject)); str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", message.From.Address)); str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";PARTSTAT=ACCEPTED:mailto:{1}", message.To[0].DisplayName, message.To[0].Address)); str.AppendLine("BEGIN:VALARM"); str.AppendLine("TRIGGER:-PT15M"); str.AppendLine("ACTION:DISPLAY"); str.AppendLine("DESCRIPTION:Reminder"); str.AppendLine("END:VALARM"); str.AppendLine("END:VEVENT"); str.AppendLine("END:VCALENDAR"); System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar"); ct.Parameters.Add("method", "PUBLISH"); AlternateView avCal = AlternateView.CreateAlternateViewFromString("abc", ct); message.AlternateViews.Add(avCal); client.Send(message); 解决方案 You cannot send an email as "localhost" as your SMTP client. That has to be legitimate in order for an email to be sent.It has nothing to do with anything in your code except SmtpClient client = new SmtpClient("localhost");. It assumes that you are running SMTP server on your local host. I doubt you do. This parameter should be the host computer name or IP address of the host running some SMTP service.—SASystem.Net.Mail.MailMessage meg = new System.Net.Mail.MailMessage();System.Net.Mail.SmtpClient mailsender = new System.Net.Mail.SmtpClient();try { string su ="Your Message" string attachfilepath = ""; //apppath get path for file if (File.Exists(apppath + "\\ErrorLogs.txt") == true) { attachfilepath = apppath + "\\ErrorLogs.txt"; } else if (File.Exists(apppath + "\\ErrorLogs.txt") == true) { attachfilepath = apppath + "\\ErrorLogs.txt"; } Attachment atm = new Attachment(attachfilepath); meg.From = new MailAddress("From Address", "SendeName"); // Email address of the sender and Display name(optional) meg.To.Add("[email protected]"); //To address meg.CC.Add("[email protected]"); //Cc meg.Bcc.Add("[email protected]"); //Bcc meg.Subject = "This is text mail"; // Email subject meg.Body = su; //Email body i.e. the content of the email meg.IsBodyHtml = true; meg.Attachments.Add(atm); //Attachment Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath + "/web.config"); MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings"); if (mailSettings != null) { mailsender.Host = mailSettings.Smtp.Network.Host; mailsender.Port = mailSettings.Smtp.Network.Port; if (Convert.ToBoolean(mailSettings.Smtp.Network.DefaultCredentials)) { mailsender.UseDefaultCredentials = true; } if (Convert.ToBoolean(mailSettings.Smtp.Network.EnableSsl)) { mailsender.EnableSsl = true; } mailsender.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password); } mailsender.Send(meg);} catch (Exception ex) { ShowMessage(ex.Message);} finally { ShowMessage("Mail Successfully send");}for best coding try to give email address and password in web.config filehere the code for web.config<pre lang="xml"> <system.net> <mailSettings> <smtp> <network defaultCredentials="true" host="smtp.gmail.com" password="password" port="587" userName="emailaddress" enableSsl="true"/> </smtp> </mailSettings> <settings> <httpWebRequest useUnsafeHeaderParsing="true"/> </settings> </system.net></pre>try to change port 465 to 587 这篇关于.NET发送电子邮件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 00:46