C# 通过exchange发送邮件 defaultuser and use username_password
来源:互联网 发布:彩蝶排课软件 编辑:程序博客网 时间:2024/06/06 02:33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace testMail
{
classProgram
{
staticvoid Main(string[] args)
{
List<string> to = newList<string>();
to.Add("v-yizu@microsoft.com");
to.Add("v-shahui@microsoft.com");
SendExchangeEmails("v-shahui","","v-shahui@microsoft.com", to, "mail.microsoft.com","mail test","body test",true);
SendMailUse();
}
publicstaticvoid SendMailUse()
{
string host = "smtp.office365.com";
string userName = "v-shahui@microsoft.com";
string password = "asd521..";
SmtpClient client = newSmtpClient();
client.DeliveryMethod =SmtpDeliveryMethod.Network;
client.Host = host;
client.UseDefaultCredentials =true;
client.Credentials =new System.Net.NetworkCredential(userName, password);
client.EnableSsl =true;
client.Port = 587;
string strfrom = userName;
string strto = "v-yizu@microsoft.com";
string strcc = "v-shahui@microsoft.com";
string subject = "test title";
string body = "test mail body";
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From =newMailAddress(strfrom,"xyf");
msg.To.Add(strto);
msg.CC.Add(strcc);
msg.Subject = subject;
msg.Body = body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml =true;
msg.Priority =MailPriority.High;
try
{
client.Send(msg);
Console.WriteLine("发送成功");
}
catch (System.Net.Mail.SmtpException ex)
{
Console.WriteLine(ex.Message,"发送邮件出错");
}
}
publicstaticvoid SendExchangeEmails(string credentialUserName, string credentialUserPwd, string fromEmail, List<string> recipientEmail, string domainName, string subjectName, string bodyVal, bool isHtml)
{
MailMessage message = newMailMessage();
MailAddress fromAddress = newMailAddress(fromEmail);
foreach (string item in recipientEmail)
{
message.To.Add(item);
}
message.From = fromAddress;
message.Subject = subjectName;
message.Body = bodyVal;
message.IsBodyHtml = isHtml;
#region Add attachments
//if (fileArray != null)
//{
// for (int i = 0; i < fileArray.Length; i++)
// {
// 为邮件创建文件附件
// Attachment attr = new Attachment(fileArray[i], MediaTypeNames.Application.Octet);
// 添加邮件时间戳信息
// ContentDisposition conDispositon = attr.ContentDisposition;
// conDispositon.CreationDate = System.IO.File.GetCreationTime(fileArray[i]);//文件的创建日期
// conDispositon.ModificationDate = System.IO.File.GetLastWriteTime(fileArray[i]);//文件的修改日期
// conDispositon.ReadDate = System.IO.File.GetLastAccessTime(fileArray[i]);//文件的读取日期
// 给邮件添加附件
// message.Attachments.Add(attr);
// }
//}
#endregion
try
{
SmtpClient smtpClient = newSmtpClient();
smtpClient.Timeout = 50000;
smtpClient.Host = domainName;
smtpClient.Port = 465;
smtpClient.UseDefaultCredentials =true;
//smtpClient.Credentials = new System.Net.NetworkCredential(credentialUserName, credentialUserPwd);
smtpClient.EnableSsl =true;
smtpClient.DeliveryMethod =SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { returntrue; };
smtpClient.Send(message);
smtpClient.Dispose();
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadLine();
}
}
}
}
- C# 通过exchange发送邮件 defaultuser and use username_password
- c# exchange 邮件发送
- 通过SmtpClient发送Exchange会议邮件
- 通过SmtpClient发送Exchange会议邮件
- C#通过SMTP发送邮件
- C#通过Outlook发送邮件
- Exchange发送邮件
- C# 发送邮件示例代码(GMail和Exchange)
- C# winform 通过调用outlook发送邮件
- C#中通过SMTP发送邮件
- 使用C#通过GMAIL发送邮件.
- C#中通过smtp发送邮件
- 使用Exchange读取邮件、发送邮件
- Excel Exchange and C#
- Excel Exchange and C#
- C# 通过smtp服务器进行邮件发送 MailHelper
- 通过C#发送自定义的html格式邮件
- 通过SOCKET发送邮件
- 获取当前jar包的绝对路径
- 欢迎使用CSDN-markdown编辑器
- 查看表空间使用情况
- JUC教程
- nginx FastCGI错误Primary script unknown解决办法(访问php提示"File not found.)
- C# 通过exchange发送邮件 defaultuser and use username_password
- 十张思维导图掌握javascript重点
- fresco加载图片+retrofit网络请求+rxjava2+xrecyclerview下拉刷新上拉加载
- 数据结构-查找-顺序查找(1)时间复杂度(n+1)/2
- VS2015 C# System.Data.SQLite
- Android 之路12---Java基础7
- 【iOS】自定义CALayer可能会出现没有动画过程但有动画结果的解析
- CUDA
- 改变viewPager滑动的速度,利用反射完美实现