博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#发送邮件
阅读量:6980 次
发布时间:2019-06-27

本文共 1364 字,大约阅读时间需要 4 分钟。

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啦

转载于:https://www.cnblogs.com/xiexingen/p/3900536.html

你可能感兴趣的文章
欧拉回路
查看>>
AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203
查看>>
MongoDB中ObjectId的误区,以及引起的一系列问题
查看>>
参与2011年7月13日举行的Azure国际猜拳锦标赛,赢取5,000美元大奖
查看>>
设计模式学习2 工厂模式
查看>>
C++2年经验
查看>>
【阿圆实验】Consul HA 高可用方案
查看>>
Android如何防止apk程序被反编译
查看>>
免费的私人代码托管(bitbucket) 和 常用git指令
查看>>
手机端head部分
查看>>
对象Equals相等性比较的通用实现
查看>>
codeforces 876 D. Sorting the Coins
查看>>
maven学习(4)-Maven 构建Web 项目
查看>>
java高并发编程(二)
查看>>
【转】unity3d 在UGUI中制作自适应调整大小的滚动布局控件
查看>>
Java 对synchronized的补充Lock锁
查看>>
Collection集合List、Set
查看>>
Effective java 43返回零长度的数组或者集合而不是null
查看>>
Spring学习系列(二) 自动化装配Bean
查看>>
学习Mybatis与mysql数据库的示例笔记
查看>>