• 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 }
  • 相关阅读:
    设置标题自适应宽度,动态调整大小
    终止延迟函数
    iOS 关于音频开发
    阻止iOS设备锁屏
    苹果开发——设置iTunes Connect中的Contracts, Tax, and Banking
    【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
    uibutton 设置title 居左显示
    通过view 获取viewController
    tableview 自动滑动到某一行
    uibutton 设置圆角边框
  • 原文地址:https://www.cnblogs.com/weitangmonkey/p/5211188.html
Copyright © 2020-2023  润新知