博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用C#和Java发送邮件(转载)
阅读量:4355 次
发布时间:2019-06-07

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

做用户注册时免不了要让用户使用邮件激活账号,这时就得用到使用程序发邮件的技术了。如下是我在项目中常用的发邮件的方法:

【C#】

1     using System.Net.Mail;  2     using System.Net;  3  4     public class EmailEntity  5     {
6 private MailMessage mm; 7 8 /// 9 /// 发送邮件 10 /// 11 public void sendMessage() 12 {
13 14 //指定发件人的邮箱地址 15 MailAddress fromAddress = new MailAddress("abc@163.com"); 16 //指定收件人的邮箱地址 17 MailAddress toAddress = new MailAddress("efg@qq.com"); 18 //创建邮件对象 19 mm = new MailMessage(fromAddress, toAddress); 20 //添加一个密送的邮件地址 21 mm.Bcc.Add(new MailAddress("xiuxiu@qq.com")); 22 //添加一个抄送的邮件地址 23 mm.CC.Add(new MailAddress("me@qq.com")); 24 //指定邮件标题的编码格式 25 mm.SubjectEncoding = Encoding.UTF8; 26 //设置邮件标题 27 mm.Subject = "我是标题"; 28 //指定邮件正文编码 29 mm.BodyEncoding = Encoding.UTF8; 30 //指定邮件正文是否text/html类型 31 mm.IsBodyHtml = true; 32 //设置邮件正文内容 33 mm.Body = "我爱秀秀!"; 34 //创建附件 35 Attachment file = new Attachment(AppDomain.CurrentDomain.BaseDirectory+"xiuxiu.jpg"); 36 //设置附件的创建日期 37 file.ContentDisposition.CreationDate = DateTime.Now; 38 //设置附件的类型 39 file.ContentType = new System.Net.Mime.ContentType("image/jpeg;charset=UTF-8"); 40 //将附件添加到邮件中 41 mm.Attachments.Add(file); 42 43 //指定邮件发送服务器的地址和端口号 44 SmtpClient sc = new SmtpClient("smtp.163.com", 25); 45 //指定发件人的身份验证信息 46 sc.Credentials = new NetworkCredential("abc", "123456"); 47 //sc.Send(mm); 48 //注册异步发送事件 49 sc.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted); 50 //开始执行异步发送邮件 51 sc.SendAsync(mm, null); 52 } 53 //异步发送邮件完成时处理事件 54 void sc_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 55 {
56 if (mm != null) 57 {
58 mm.Dispose();//释放资源 59 } 60 if (e.Cancelled) 61 {
62 Console.WriteLine("用户已取消!"); 63 } 64 else if (e.Error != null) 65 {
66 Console.WriteLine("发送失败,原因如下:" + e.Error.Message); 67 } 68 else 69 {
70 Console.WriteLine("发送成功!"); 71 } 72 } 73 }

【Java】

1 import java.util.Date;   2 import java.util.Properties;   3 import javax.mail.Authenticator;   4 import javax.mail.PasswordAuthentication;   5 import javax.mail.Message.RecipientType;   6 import javax.mail.MessagingException;   7 import javax.mail.Session;   8 import javax.mail.Transport;   9 import javax.mail.internet.AddressException;  10 import javax.mail.internet.InternetAddress;  11 import javax.mail.internet.MimeMessage;  12 import javax.mail.internet.MimeMultipart;  13  14 /**  15  * 邮件实体类,封装了有关发送 邮件的方法。  16  * @author xxxxxx  17  *  18  */  19 public class EmailEntity {
20 21 private MimeMessage msg; 22 23 /** 24 * 初始化邮件实体 25 * 26 * @param fromDomin 27 * 发件人邮件服务器域名,如:163.com、qq.com 28 * @param userName 29 * 发件人用户名,如:qmail、20082345 30 * @param password 31 * 发件人密码 32 * @param toAddress 33 * 收件人邮箱地址,如:200712345@qq.com 34 */ 35 public EmailEntity(String fromDomin, String userName, String password) {
36 Properties p = new Properties(); 37 //设置邮件发送服务器的地址 38 p.setProperty("mail.host", "smtp." + fromDomin); 39 //设置使用权限验证 40 p.setProperty("mail.smtp.auth", "true"); 41 //设置用户身份验证凭据 42 Session ses = Session.getDefaultInstance(p, new MyAuthenticator(userName, password)); 43 //ses.setDebug(true);//设置是否出现回显信息 44 //创建邮件实体 45 msg = new MimeMessage(ses); 46 try {
47 //设置发件人邮箱地址 48 msg.setFrom(new InternetAddress(userName + "@" + fromDomin)); 49 } catch (AddressException e) {
50 e.printStackTrace(); 51 } catch (MessagingException e) {
52 e.printStackTrace(); 53 } 54 } 55 56 /** 57 * 发送消息 58 * 59 * @param title 60 * 邮件标题 61 * @param content 62 * 邮件正文 63 * @param type 64 * 正文的类型,如:text/html、text/plain 65 * @return 66 */ 67 public boolean sendMessage(String toAddress, String title, String content, String type) {
68 69 try {
70 //设置发件人邮箱地址 71 msg.setRecipient(RecipientType.TO, new InternetAddress(toAddress)); 72 //设置邮件发送日期 73 msg.setSentDate(new Date()); 74 //设置邮件标题 75 msg.setSubject(title); 76 //设置邮件正文 77 msg.setContent(content, type); 78 //开始发送邮件 79 Transport.send(msg); 80 return true; 81 } catch (MessagingException ex) {
82 ex.printStackTrace(); 83 return false; 84 } 85 } 86 87 /** 88 * 发送带附件的邮件 89 * @param toAddress 收件人的邮箱地址,如 90 * @param title 邮件标题 91 * @param content 邮件正文,包括附件 92 * @param type 邮件正文的类型 93 * @return 94 */ 95 public boolean sendEmailWithAttachment(String toAddress, String title, MimeMultipart content, String type) {
96 try {
97 msg.setRecipient(RecipientType.TO, new InternetAddress(toAddress)); 98 msg.setSentDate(new Date()); 99 msg.setSubject(title); 100 msg.setContent(content); 101 Transport.send(msg); 102 return true; 103 } catch (MessagingException ex) {
104 ex.printStackTrace(); 105 return false; 106 } 107 } 108 } 109 110 //用户身份验证凭据类 111 class MyAuthenticator extends Authenticator {
112 113 private String _userName; 114 private String _password; 115 116 public MyAuthenticator(String userName,String password){
117 this._userName = userName; 118 this._password = password; 119 } 120 121 @Override 122 public PasswordAuthentication getPasswordAuthentication() {
123 //返回使用了用户名和密码的身份验证凭据 124 return new PasswordAuthentication(_userName, _password); 125 } 126 }

经过测试,均测试成功!如下:

转载于:https://www.cnblogs.com/ycxyyzw/archive/2011/07/18/2109845.html

你可能感兴趣的文章
(原创) cocos2d-x 3.0+ lua 学习和工作(4) : 公共函数(3): 深度克隆clone()
查看>>
为什么写作
查看>>
整数子数组求最大和添加验证
查看>>
使用kubeadm安装Kubernetes
查看>>
Principal Component Analysis 主元分析
查看>>
linux分割字符串操作
查看>>
PHP学习2
查看>>
多实例Mysql配置
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>
hdu4348区间更新的主席树+标记永久化
查看>>
ZOJ 2532 Internship
查看>>
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>
图的遍历 | 1013 连通块块数
查看>>
Kinect 开发 —— 进阶指引(上)
查看>>
python学习笔记(六)time、datetime、hashlib模块
查看>>
uva489(需要考虑周全)
查看>>
C-关键字(二)
查看>>
排序笔记
查看>>