用C#发送邮件的编程方法及实例代码分享
在此分享一个c#发送邮件的实例代码,代码是亲自写出来,而且也亲测过气动阀门,可以正常发送邮件。希望对大家有所帮助!
为了方便使用,麦子把发邮件的核心代码提取为一个气动阀门类(Mail),
代码如下:
02 |
using System.Collections.Generic; |
05 |
using System.Net.Mail; |
06 |
using System.ComponentModel; |
07 |
using System.Windows.Forms; |
10 |
namespace com.ykmaiz.email |
14 |
private string username = ""; |
15 |
private string password = ""; |
16 |
private string domain = ""; |
17 |
public Mail(string username,string password,string domain) |
19 |
this.username = username; |
20 |
this.password = password; |
23 |
public void send(string from,string [] to,string [] cc,string title,stringcontent) |
25 |
MailMessage mailMsg = new MailMessage(); |
26 |
mailMsg.From = new MailAddress(from); |
29 |
foreach(string s in to) |
36 |
foreach (string s in cc) |
41 |
mailMsg.Subject = title; |
42 |
mailMsg.Body = content; |
43 |
mailMsg.BodyEncoding = Encoding.UTF8; |
44 |
mailMsg.IsBodyHtml = false; |
45 |
mailMsg.Priority = MailPriority.High; |
46 |
SmtpClient smtp = new SmtpClient(); |
47 |
smtp.Credentials = new NetworkCredential(username, password); |
50 |
smtp.EnableSsl = false; |
51 |
smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted); |
54 |
smtp.SendAsync(mailMsg, mailMsg); |
56 |
catch (SmtpException ex) |
58 |
Console.WriteLine(ex.ToString()); |
|
然后直接调用气动阀门该类的send()方法即可,
实例代码如下:
1 |
Mail mail = new Mail("发邮件的地址", "发邮件的密码", "邮件的smtp地址"); |
3 |
mail.send("发邮件的地址", new string[] { "收件人地址" }, new string[] { "抄送人地址" },"邮件标题", "邮件内容"); |
|
很方便,赶快去试试吧!
以后会陆续更新我的气动阀门经验分享,请多关注!