C#发送邮件
2014-08-09 09:08
148 查看
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服务才行,于是乎(看下图)
保存后,ok啦
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服务才行,于是乎(看下图)
保存后,ok啦
相关文章推荐
- C#使用CDO发送邮件
- C#使用CDO发送邮件
- C#发送邮件
- c#.net 下的邮件发送(含附件)
- C#使用CDO发送邮件
- c#中使用自带的邮件发送组件发送邮件~
- c#jmail发送邮件(摘)
- 在C#编写代码发送邮件
- C# 创建邮件发送组件(SMTP)(转)
- C#批量发送邮件程序
- C#发送邮件
- c#发送需要smtp认证的邮件
- c#发送需要smtp认证的邮件
- C# 发送邮件
- 转载自http://www.xuanxi.net/Article/Catalog6/10748.html的C#写邮件发送程序
- C#使用CDO发送邮件
- C# - MailSender 邮件发送组件源代码 (支持ESMTP, 附件)
- 下面是几段C#发送邮件相关的代码,我也懒得说明了,就这么个意思。
- C#发送邮件程序 及测试邮件服务器
- C#批量发送邮件程序