asp.net 发送邮件函数两则

 2023-09-05 阅读 18 评论 0

摘要:1 using System.Net.Mail; 2 using System.Text; 3 using System.Net; 4 #region 邮件发送 5 /// <summary> 6 /// 邮件发送 7 /// </summary> 8 /// <param name="str">字符串</param> 9 /// <returns></returns> 10 publi
 1 using System.Net.Mail;    
 2 using System.Text;
 3 using System.Net;
 4    #region 邮件发送
 5         /// <summary>
 6         /// 邮件发送
 7         /// </summary>
 8         /// <param name="str">字符串</param>
 9         /// <returns></returns>
10         public static string SendMail(string mailtitle, string mailcontent, string toemail, string toname)
11         {
12             ////设置发件人信箱,及显示名字
13             MailAddress from = new MailAddress("xxx@xxx.com", "xxx");
14             //设置收件人信箱,及显示名字
15             MailAddress to = new MailAddress(toemail, toname);
16             //创建一个MailMessage对象
17             MailMessage oMail = new MailMessage(from, to);
18             oMail.Subject = mailtitle; //邮件标题
19             oMail.Body = mailcontent; //邮件内容
20             oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
21             oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
22             oMail.Priority = MailPriority.High;//设置邮件的优先级为高
23             //发送邮件服务器
24             SmtpClient client = new SmtpClient();
25             client.Host = "mail.xxxx.com"; //指定邮件服务器
26             client.Credentials = new NetworkCredential("xxx@xxxx.com", "xxxxx");//指定服务器邮件,及密码
27             //发送
28             try
29             {
30                 client.Send(oMail); //发送邮件
31                 oMail.Dispose(); //释放资源
32                 return "1";
33             }
34             catch (Exception ex)
35             {
36                 oMail.Dispose(); //释放资源
37                 return ex.Message;
38             }
39         }
40         #endregion
41         #region   自定义邮件发送
42         /// <summary>
43         /// 邮件发送
44         /// </summary>
45         /// <param name="str">字符串</param>
46         /// <returns></returns>
47         public static string SendMail(string fromEmail,string fromName,string host,string username,string password,string mailtitle, string mailcontent, string toemail, string toname)
48         {
49             ////设置发件人信箱,及显示名字
50             MailAddress from = new MailAddress(fromEmail, fromName);
51             //设置收件人信箱,及显示名字
52             MailAddress to = new MailAddress(toemail, toname);
53             //创建一个MailMessage对象
54             MailMessage oMail = new MailMessage(from, to);
55             oMail.Subject = mailtitle; //邮件标题
56             oMail.Body = mailcontent; //邮件内容
57             oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
58             oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
59             oMail.Priority = MailPriority.High;//设置邮件的优先级为高
60             //发送邮件服务器
61             SmtpClient client = new SmtpClient();
62             client.Host = host; //指定邮件服务器
63             client.Credentials = new NetworkCredential(username,password);//指定服务器邮件,及密码
64             //发送
65             try
66             {
67                 client.Send(oMail); //发送邮件
68                 oMail.Dispose(); //释放资源
69                 return "1";
70             }
71             catch (Exception ex)
72             {
73                 oMail.Dispose(); //释放资源
74                 return ex.Message;
75             }
76         }
77         #endregion

 

转载于:https://www.cnblogs.com/Chaser-Eagle/p/3684595.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/1795.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息