zoukankan      html  css  js  c++  java
  • 使用C#和Java发送邮件(转载)

    1     using System.Net.Mail;
     2     using System.Net;
     3 
     4     public class EmailEntity
     5     {
     6         private MailMessage mm;
     7 
     8         /// <summary>
     9         /// 发送邮件
    10         /// </summary>
    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 = "<span style='color:#ff0000;font-weight:bold;'>我爱秀秀!</span>";
    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     }
    View Code
     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 收件人的邮箱地址,如bob@126.com
     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 }
    View Code

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

  • 相关阅读:
    vue3+typescript引入外部文件
    vue项目中使用sass
    关于Vue.use()使用详解
    案例:密码框格式提示信息错误
    案例:显示隐藏文本框里面的内容
    案例:循环精灵图案例
    案例:关闭淘宝二维码案例
    案例: 仿京东显示隐藏密码
    案例:根据系统时间显示不同的问候语
    ES6中类和对象的注意问题
  • 原文地址:https://www.cnblogs.com/shangshen/p/3450644.html
Copyright © 2011-2022 走看看