zoukankan      html  css  js  c++  java
  • java 邮件Demo

    Demo1:

    package cn.itcastl.javamail2;
    
    
    import java.util.Properties;
    
    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class Demo1 {
    
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception{
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.transport.protocol", "smtp");     
            Session session =Session.getInstance(properties);
            session.setDebug(true);
            
            Message msg = new MimeMessage(session);
            msg.setText("你111111111好!!!");
            msg.setFrom(new InternetAddress("tangweishx@163.com"));
            
            Transport transport = session.getTransport();
            transport.connect("smtp.163.com", 25, "用户名", "密码");
            transport.sendMessage(msg, new Address[]{new InternetAddress("收件邮箱")} );
            transport.close();
        }
    
    }

    Demo2:

     1 package cn.itcastl.javamail2;
     2 
     3 import java.util.Properties;
     4 
     5 import javax.mail.Message;
     6 import javax.mail.Message.RecipientType;
     7 import javax.mail.Authenticator;
     8 import javax.mail.PasswordAuthentication;
     9 import javax.mail.Session;
    10 import javax.mail.Transport;
    11 import javax.mail.internet.InternetAddress;
    12 import javax.mail.internet.MimeMessage;
    13 
    14 public class Demo2 {
    15 
    16     /**
    17      * @param args
    18      */
    19     public static void main(String[] args) throws Exception{
    20         Properties props = new Properties();
    21         props.setProperty("mail.smtp.auth", "true");     
    22         props.setProperty("mail.transport.protocol", "smtp");
    23         props.setProperty("mail.host", "smtp.163.com");
    24         
    25         Session session = Session.getInstance(props, 
    26                     new Authenticator() {
    27                         protected PasswordAuthentication getPasswordAuthentication(){
    28                             return new PasswordAuthentication("邮箱用户名","密码");
    29                         }
    30                     }
    31                 );
    32         session.setDebug(true);
    33         Message msg = new MimeMessage(session);
    34         msg.setFrom(new InternetAddress("XXXXX@163.com"));
    35         msg.setSubject("测试");
    36         msg.setRecipients(RecipientType.TO, InternetAddress.parse("收件人1地址,收件人2地址"));
    37         msg.setContent("<span style='color:red'>测试类顶顶顶顶</span>", "text/html;charset=gbk");
    38         
    39         Transport.send(msg);     
    40         
    41     }
    42 
    43 }

     Demo3:

     1 package cn.itcastl.javamail2;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.io.OutputStream;
     7 import java.util.Properties;
     8 
     9 import javax.activation.DataHandler;
    10 import javax.activation.DataSource;
    11 import javax.activation.FileDataSource;
    12 import javax.mail.Address;
    13 import javax.mail.Message;
    14 import javax.mail.MessagingException;
    15 import javax.mail.Session;
    16 import javax.mail.internet.InternetAddress;
    17 import javax.mail.internet.MimeBodyPart;
    18 import javax.mail.internet.MimeMessage;
    19 import javax.mail.internet.MimeMessage.RecipientType;
    20 import javax.mail.internet.MimeMultipart;
    21 import javax.mail.internet.MimeUtility;
    22 
    23 public class Demo3 {
    24 
    25     /**
    26      * @param args
    27      * @throws MessagingException 
    28      * @throws IOException 
    29      */
    30     public static void main(String[] args) throws MessagingException, IOException {
    31         Session session = Session.getInstance(new Properties());
    32         Message msg = new MimeMessage(session);
    33         MimeMultipart msgMultipart = new MimeMultipart("mixed");
    34         msg.setContent(msgMultipart);
    35         msg.setSubject("测试1");
    36         msg.setFrom(new InternetAddress("""+MimeUtility.encodeText("糖糖糖")+"" <tangwie@163.com>"));
    37         msg.setReplyTo(new Address[]{new InternetAddress("llasdfsdlll@163.com")});
    38         msg.setRecipients(RecipientType.TO, InternetAddress.parse(MimeUtility.encodeText("唐伟") +" <tangweiii@163.com>,"+MimeUtility.encodeText("维棠") +" <weitang@163.com>"));
    39         MimeBodyPart attch1 = new MimeBodyPart();
    40         MimeBodyPart attch2 = new MimeBodyPart();
    41         MimeBodyPart content = new MimeBodyPart();
    42         attch1.setFileName("java"+MimeUtility.encodeText("测试")+".txt");
    43         attch2.setFileName("bd_logo1.png");
    44         
    45         msgMultipart.addBodyPart(attch1);
    46         msgMultipart.addBodyPart(attch2);
    47         msgMultipart.addBodyPart(content);
    48         
    49         DataSource ds = new FileDataSource("C:\Users\Administrator\Desktop\java.txt");
    50         DataHandler dh1 = new DataHandler(ds);
    51         attch1.setDataHandler(dh1);
    52         
    53         DataSource ds2 = new FileDataSource("C:\Users\Administrator\Desktop\bd_logo1.png");
    54         DataHandler dh2 = new DataHandler(ds2);
    55         attch2.setDataHandler(dh2);
    56         
    57         MimeMultipart bodyMultipart = new MimeMultipart("relater");
    58         content.setContent(bodyMultipart);
    59         MimeBodyPart htmlPart = new MimeBodyPart();
    60         MimeBodyPart gifPart = new MimeBodyPart();
    61         bodyMultipart.addBodyPart(htmlPart);
    62         bodyMultipart.addBodyPart(gifPart);
    63         
    64         DataSource gifds = new FileDataSource("C:\Users\Administrator\Desktop\123.jpg");
    65         DataHandler gifdh = new DataHandler(gifds );
    66         gifPart.setDataHandler(gifdh );
    67         gifPart.setHeader("Content-Location", "https://www.baidu.com/img/bd_logo1.png22");
    68         
    69         htmlPart.setContent("测试测试!!!<img src='https://www.baidu.com/img/bd_logo1.png'>", "text/html;charset=gbk");
    70         
    71         msg.saveChanges();
    72         
    73         OutputStream ops = new FileOutputStream("C:\Users\Administrator\Desktop\demo.eml");
    74         msg.writeTo(ops );
    75         ops.close();
    76     }
    77 
    78 }
  • 相关阅读:
    2018 ACM 网络选拔赛 徐州赛区
    2018 ACM 网络选拔赛 焦作赛区
    2018 ACM 网络选拔赛 沈阳赛区
    poj 2289 网络流 and 二分查找
    poj 2446 二分图最大匹配
    poj 1469 二分图最大匹配
    poj 3249 拓扑排序 and 动态规划
    poj 3687 拓扑排序
    poj 2585 拓扑排序
    poj 1094 拓扑排序
  • 原文地址:https://www.cnblogs.com/weitangmonkey/p/5211188.html
Copyright © 2011-2022 走看看