Java发送mail和C#发送mail
Java发送mail
阿里云邮箱,配置公司邮箱服务器,邮箱地址,授权码(运维同事提供,听说阿里云邮箱的授权码和密码一样),端口465,测试能发送。
/**
* 发送简单的文本邮件
*/
public static boolean sendTextEmail(String to, String title, String msg) throws Exception {
if (to.trim().equals("")) {
return false;
}
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(USERNAME, PERSONAL, DEFAULT_ENCODING));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.trim()));
message.setSubject(title);
message.setText(msg);
message.setSentDate(new Date());
message.saveChanges();
Transport trans = session.getTransport("smtp");
trans.send(message);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
C#发送邮箱
跟上面配一样的公司邮箱服务器,邮箱地址,授权码(运维同事提供,听说阿里云邮箱的授权码和密码一样),端口465,测试发送失败。
改成自己的QQ邮箱地址及服务器,授权码,端口仍是465,依然失败,网上查了说是net.mail不支持ssl加密发送;
再次尝试,改成Web.mail方式发送,QQ邮箱smtpusessl=true,端口465,成功,代码如下;smtpusessl=false,端口25,也成功,这个代码就放了,就改两处。
//发件人
const string sendAddress = "xxxx@qq.com";
//发件人密码
const string sendPassword = "xxxx";
//收件人
const string receiveAddress = "xxxx@xxxxx.com";
//服务器
const string host = "smtp.qq.com";
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
try
{
mail.From = sendAddress;
mail.To = receiveAddress;
mail.Subject = "测试";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = "dxd发送的测试邮件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", sendAddress); //set your username here 发件人邮箱
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendPassword); //set your password here 发件人邮箱密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//发送端口号
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//是否启用ssl
//需要引用组件System.Web
System.Web.Mail.SmtpMail.SmtpServer = host;
System.Web.Mail.SmtpMail.Send(mail);
Console.WriteLine("发送邮件成功");
}
catch (Exception ex)
{
var exg = ex.InnerException ?? ex;
string msg = exg.ToString();
Console.WriteLine(msg);
}
改成公司的邮箱配置,smtpusessl=false,端口25,也成功;smtpusessl=true,端口465,失败,很费解;
Web.mail,公司的邮箱配置,smtpusessl=false,端口25,不是到阿里云服务器,测试,发送失败,网上查了说是阿里云25端口被禁,可以申请解禁,流程好像比较麻烦。
改成465,仍然失败,修改smtpusessl=true,仍然失败。
正当一筹莫展时,看到之前net.mail发送端口配的是587,尝试改成net,mail发送,仍然用公司的邮箱配置,EnableSsl = false,端口改成587,居然成功了;
再次尝试,把EnableSsl = true,测试失败,提示:根据验证过程,远程证书无效,网上查了:http://t.zoukankan.com/yifengjianbai-p-6128396.html,
说要加一段代码:
ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
加上再次测试,成功,完全体代码如下:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace RNCloud
{
public class MailHelper
{
private static string host = ConfigurationManager.AppSettings["email:host"];
private static string port = ConfigurationManager.AppSettings["email:port"];
private static string username = ConfigurationManager.AppSettings["email:username"] ;
private static string password = ConfigurationManager.AppSettings["email:password"];
#region Net.Email发送
/// <summary>
/// 发送电子邮件邮件
/// </summary>
/// <param name="to">接收电子邮件地址</param>
/// <param name="bcc">暗送电子邮件地址,可为空</param>
/// <param name="cc">抄送电子邮件地址,可为空</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
/// <param name="mailatta">附件</param>
/// <param name="bodyType">邮件正文格式</param>
public static void SendNetMail(string[] to, string[] bcc, string[] cc, string subject, string body, System.Net.Mail.Attachment mailatta, string bodyType)
{
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = host; //指定SMTP服务器
smtpClient.Port = port.ToInt();//指定SMTP端口
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);//用户名和密码
System.Net.Mail.MailMessage m_Mail = new System.Net.Mail.MailMessage();
//邮件编码
m_Mail.BodyEncoding = System.Text.Encoding.UTF8;
//设置发件人
System.Net.Mail.MailAddress fromAddr = new System.Net.Mail.MailAddress(username);
m_Mail.From = fromAddr;
//收件人
if (to != null)
foreach (string s_to in to)
{
m_Mail.To.Add(s_to);
}
//秘密抄送人
if (bcc != null)
foreach (string s_bcc in bcc)
{
m_Mail.Bcc.Add(s_bcc);
}
//抄送人
if (cc != null)
foreach (string s_cc in cc)
{
m_Mail.CC.Add(s_cc);
}
//定义邮件的主题
m_Mail.Subject = subject;
//定义邮件的主体
m_Mail.Body = body;
//'邮件以 HTML的格式发送
m_Mail.IsBodyHtml = bodyType.Equals("html");
//定义邮件的优先级,在此设定为高
m_Mail.Priority = System.Net.Mail.MailPriority.High;
// 给发送的邮件附加上一个附件
if (mailatta != null)
{
m_Mail.Attachments.Add(mailatta);
}
try
{
//加这段之前用公司邮箱发送报错:根据验证过程,远程证书无效
//加上后解决问题
ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
smtpClient.Send(m_Mail);
}
catch (Exception e)
{
throw e;
}
}
public static void SendNetMail(string to, string subject, string text)
{
SendNetMail(new string[] { to }, null, null, subject, text, null, "html");
}
#endregion
}
}
记录下尝试的过程,防止忘记,再次感谢:http://t.zoukankan.com/yifengjianbai-p-6128396.html

浙公网安备 33010602011771号