#region 发送邮件部分
private
static
String fromMail =
"1111@126.com"
;
///邮箱名称
private
static
String mailPwd =
"111111"
;
///密码
private
static
string
toMail =
"2222@163.com"
;
///邮箱服务器
private
static
string
fileStr;
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="fileUrl">附件地址,以~分</param>
/// <param name="screen">是否发送截屏</param>
/// <returns></returns>
public
static
string
SendMail(
string
fileUrl,
string
screen)
{
MailAddress
from
=
new
MailAddress(fromMail);
MailAddress to =
new
MailAddress(toMail);
MailMessage message =
new
MailMessage(
from
, to);
message.Subject =
"M邮件 "
+11111;
message.SubjectEncoding = System.Text.Encoding.UTF8;
Attachment attachFile =
new
Attachment(fileStr);
if
(screen ==
"True"
)
message.Attachments.Add(attachFile);
string
[] files = fileUrl.Split(
'~'
);
for
(
int
i = 0; i < files.Length; i++)
{
if
(File.Exists(files[i]))
{
Attachment attachFile1 =
new
Attachment(fileUrl);
message.Attachments.Add(attachFile1);
}
}
try
{
SmtpClient client =
new
SmtpClient(
"smtp."
+
from
.Host);
SendMail(client,
from
, mailPwd, to, message);
return
"发送邮件成功!包含附件:"
+ fileUrl +
" 含截图?"
+ screen +
" "
+ DateTime.Now.ToString();
}
catch
(SmtpException ex)
{
if
(ex.StatusCode == SmtpStatusCode.GeneralFailure)
{
try
{
SmtpClient client =
new
SmtpClient(
from
.Host);
SendMail(client,
from
, mailPwd, to, message);
return
"发送邮件成功!包含附件:"
+ fileUrl +
" 含截图?"
+ screen +
" "
+ DateTime.Now.ToString();
}
catch
(SmtpException err)
{
return
"发送邮件失败!"
+ err.Message +
" "
+ DateTime.Now.ToString();
}
}
else
{
return
"发送邮件失败!"
+ ex.Message +
" "
+ DateTime.Now.ToString();
}
}
}
private
static
void
SendMail(SmtpClient client, MailAddress
from
,
string
password,
MailAddress to, MailMessage message)
{
client.UseDefaultCredentials =
false
;
client.Credentials =
new
NetworkCredential(
from
.Address, password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(message);
}
catch
{
throw
;
}
finally
{
message.Dispose();
}
}
#endregion