您好,登录后才能下订单哦!
这篇文章主要介绍“C#怎么使用System.Net.Mail类实现邮件发送”,在日常操作中,相信很多人在C#怎么使用System.Net.Mail类实现邮件发送问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么使用System.Net.Mail类实现邮件发送”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
有些 SMTP 服务器要求在代表客户端发送电子邮件前验证客户端的身份。当此 SmtpClient 对象应该使用当前登录用户的默认凭据进行身份验证(如果服务器要求)时,请将此属性设置为 true。对于客户端应用程序,这是在大多数情况下都需要的行为。也可以使用应用程序和计算机配置文件指定凭据信息。有关更多信息,请参见 <mailSettings> 元素(网络设置)。
如果 UseDefaultCredentials 属性设置为 false,则连接到服务器时会将 Credentials 属性中设置的值用作凭据。如果 UseDefaultCredentials 属性设置为 false 并且尚未设置 Credentials 属性,则将邮件以匿名方式发送到服务器。UseDefaultCredentials 的默认值为false。
如果提供用于基本身份验证的凭据,则凭据将以明文形式发送到服务器。这可能会产生安全问题,因为凭据可以被他人看见从而加以利用。但是向163的邮箱发送邮件是必须要带上发送者的凭据信息,否则邮件是发送不出去的。
SmtpClient也可以这样建立:SmtpClient client=new SmtpClient(smtp_server)
,因为邮件发送服务器的默认端口是25。
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.qq.com"; int port = 25; string mail_from = "***from_mail_username***@qq.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@163.com"; string receiver = "***to_mail_username***@"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server, port); client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***"); client.Send(message); }
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.163.com"; int port = 25; string mail_from = "***from_mail_username***@163.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@163.com"; string receiver = "***to_mail_username***"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server); client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***"); client.Send(message); }
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.163.com"; int port = 25; string mail_from = "***from_mail_username***@163.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@qq.com"; string receiver = "***to_mail_username***"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server); client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***"); client.Send(message); }
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.qq.com"; int port = 25; string mail_from = "***from_mail_username***@qq.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@qq.com"; string receiver = "***to_mail_username***"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server); client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***"); client.Send(message); }
到此,关于“C#怎么使用System.Net.Mail类实现邮件发送”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。