479
京东网上商城
C# SMTP邮件发送
邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助:
核心代码:
001
|
using System;
|
002
|
using System.Net;
|
003
|
using System.Net.Mail;
|
004
|
using System.Text;
|
005
|
006
|
namespace HC.Email
|
007
|
{
|
008
|
///
<summary>
|
009
|
///
整站邮件服务类
|
010
|
///
</summary>
|
011
|
public class EmailService
|
012
|
{
|
013
|
///
<summary>
|
014
|
///
发送邮件
|
015
|
///
</summary>
|
016
|
///
<param name="mailTo">收件人
|
017
|
///
<param name="subject">主题
|
018
|
///
<param name="body">内容
|
019
|
///
<returns></returns>
|
020
|
public static bool Send( string mailTo, string subject, string body)
|
021
|
{
|
022
|
return Send( new []
{mailTo}, null ,
subject, body, true , null );
|
023
|
}
|
024
|
025
|
///
<summary>
|
026
|
///
发送邮件
|
027
|
///
</summary>
|
028
|
///
<param name="mailTo">收件人
|
029
|
///
<param name="subject">主题
|
030
|
///
<param name="body">内容
|
031
|
///
<returns></returns>
|
032
|
public static bool Send( string []
mailTo, string subject, string body)
|
033
|
{
|
034
|
return Send(mailTo, null ,
subject, body, true , null );
|
035
|
}
|
036
|
037
|
///
<summary>
|
038
|
///
发送邮件
|
039
|
///
</summary>
|
040
|
///
<param name="mailTo">收件人
|
041
|
///
<param name="subject">主题
|
042
|
///
<param name="body">内容
|
043
|
///
<param name="attachmentsPath">附件
|
044
|
///
<returns></returns>
|
045
|
public static bool Send( string []
mailTo, string subject, string body, string []
attachmentsPath)
|
046
|
{
|
047
|
return Send(mailTo, null ,
subject, body, true ,
attachmentsPath);
|
048
|
}
|
049
|
050
|
051
|
///
<summary>
|
052
|
///
发送邮件
|
053
|
///
</summary>
|
054
|
///
<param name="mailTo">收件人
|
055
|
///
<param name="mailCcArray">抄送
|
056
|
///
<param name="subject">主题
|
057
|
///
<param name="body">内容
|
058
|
///
<param name="isBodyHtml">是否Html
|
059
|
///
<param name="attachmentsPath">附件
|
060
|
///
<returns></returns>
|
061
|
public static bool Send( string []
mailTo, string []
mailCcArray, string subject, string body, bool isBodyHtml,
|
062
|
string []
attachmentsPath)
|
063
|
{
|
064
|
try
|
065
|
{
|
066
|
var
config = ConfigHelper.GetConfig<emailconfig>();
|
067
|
if ( string .IsNullOrEmpty(config.Host)
|| string .IsNullOrEmpty(config.UserName)
||
|
068
|
string .IsNullOrEmpty(config.Port)
|| string .IsNullOrEmpty(config.Password))
|
069
|
{
|
070
|
//todo:记录日志
|
071
|
return false ;
|
072
|
}
|
073
|
var
@from = new MailAddress(config.MailFrom); //使用指定的邮件地址初始化MailAddress实例
|
074
|
var
message = new MailMessage(); //初始化MailMessage实例
|
075
|
//向收件人地址集合添加邮件地址
|
076
|
if (mailTo
!= null )
|
077
|
{
|
078
|
foreach ( string t in mailTo)
|
079
|
{
|
080
|
message.To.Add(t);
|
081
|
}
|
082
|
}
|
083
|
084
|
//向抄送收件人地址集合添加邮件地址
|
085
|
if (mailCcArray
!= null )
|
086
|
{
|
087
|
foreach ( string t in mailCcArray)
|
088
|
{
|
089
|
message.CC.Add(t);
|
090
|
}
|
091
|
}
|
092
|
//发件人地址
|
093
|
message.From
= @from;
|
094
|
095
|
//电子邮件的标题
|
096
|
message.Subject
= subject;
|
097
|
098
|
//电子邮件的主题内容使用的编码
|
099
|
message.SubjectEncoding
= Encoding.UTF8;
|
100
|
101
|
//电子邮件正文
|
102
|
message.Body
= body;
|
103
|
104
|
//电子邮件正文的编码
|
105
|
message.BodyEncoding
= Encoding.Default;
|
106
|
message.Priority
= MailPriority.High;
|
107
|
message.IsBodyHtml
= isBodyHtml;
|
108
|
109
|
//在有附件的情况下添加附件
|
110
|
if (attachmentsPath
!= null &&
attachmentsPath.Length > 0)
|
111
|
{
|
112
|
foreach ( string path in attachmentsPath)
|
113
|
{
|
114
|
var
attachFile = new Attachment(path);
|
115
|
message.Attachments.Add(attachFile);
|
116
|
}
|
117
|
}
|
118
|
try
|
119
|
{
|
120
|
var
smtp = new SmtpClient
|
121
|
{
|
122
|
Credentials
= new NetworkCredential(config.UserName,
config.Password),
|
123
|
Host
= config.Host,
|
124
|
Port
= Convert.ToInt32(config.Port)
|
125
|
};
|
126
|
127
|
//将邮件发送到SMTP邮件服务器
|
128
|
smtp.Send(message);
|
129
|
//todo:记录日志
|
130
|
return true ;
|
131
|
}
|
132
|
catch (SmtpException
ex)
|
133
|
{
|
134
|
//todo:记录日志
|
135
|
return false ;
|
136
|
}
|
137
|
}
|
138
|
catch (SmtpException
ex)
|
139
|
{
|
140
|
//todo:记录日志
|
141
|
return false ;
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
}
|
最后更新:2017-04-03 05:39:11