码迷,mamicode.com
首页 > 其他好文 > 详细

C#发送邮件

时间:2014-08-09 11:18:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   io   2014   ar   cti   html   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using ShopMall.Model;
using System.Diagnostics;

namespace ShopMall.Common
{
public class sendEmail
{
private readonly SmtpClient Client;

public sendEmail(siteconfig site)
{
Client = new SmtpClient
{
Host = "smtp.qq.com",
Port =25,
DeliveryMethod = SmtpDeliveryMethod.Network
};
Client.UseDefaultCredentials = false;
Client.Credentials = new NetworkCredential("1002275364@qq.com","xxxxxxxxxxxxx");
}

/// <summary>
/// 发送邮件
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <returns></returns>
public bool SendMessage(string from, string to, string subject, string body)
{
MailMessage msg = null;
bool isSent = false;
using (msg = new MailMessage(from, to, subject, body))
{
try
{
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;

Client.Send(msg);
isSent = true;
}
catch (Exception ex)
{
isSent = false;
Trace.TraceError(ex.Message);
}
return isSent;
}
}
}
}

 

在调用的时候提示mail from address must be same as authorization user,google后才知道 原来还需要邮箱开启smtp服务才行,于是乎(看下图)

bubuko.com,布布扣

保存后,ok啦

C#发送邮件,布布扣,bubuko.com

C#发送邮件

标签:blog   http   os   io   2014   ar   cti   html   

原文地址:http://www.cnblogs.com/xiexingen/p/3900536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!