java發送email(含代理方式,ssl方式,傳統方式)

來源:互聯網
上載者:User

標籤:

package spring.vhostall.com;import java.security.Security;import java.util.Date;import java.util.Properties;import javax.mail.Address;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class MailTest {/** * 通過代理髮送郵件 */    /**      * @param args     */    public static void main(String[] args) throws Exception {     MailTest test = new MailTest();     //發送郵件//     test.sendmail();     //通過代理髮送郵件     test.sendMailByProxy();     }    private void sendMailByProxy()throws Exception{          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());     //設定Proxy 伺服器      Properties props = System.getProperties();      props.setProperty("proxySet", "true");      props.setProperty("socksProxyHost", "43.82.112.214");      props.setProperty("socksProxyPort", "1080");      props.setProperty("mail.smtp.host", "smtp.163.com");      props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");      props.setProperty("mail.smtp.socketFactory.fallback", "false");      props.setProperty("mail.smtp.port", "465");      props.setProperty("mail.smtp.socketFactory.port", "465");      props.setProperty("mail.smtp.auth", "true");      props.setProperty("mail.debug", "true");      props.setProperty("mail.transport.protocol", "smtp");     final String username = "****";     final String password = "****";          //使用驗證      Session session = Session.getDefaultInstance(props,             new javax.mail.Authenticator() {                 protected PasswordAuthentication getPasswordAuthentication() {                     return new javax.mail.PasswordAuthentication(username,password);                  }              });      MimeMessage message = new MimeMessage(session);      Address address = new InternetAddress("****");      Address toAaddress = new InternetAddress("****");           message.setFrom(address);      message.setRecipient(MimeMessage.RecipientType.TO, toAaddress);      message.setSubject("測試");      message.setText("test");      message.setSentDate(new Date());      Transport transport = session.getTransport();      transport.connect("smtp.163.com","****","****");      transport.sendMessage(message, new Address[] {new InternetAddress("****")});//      Transport.send(message);      System.out.println("郵件發送!");      transport.close();          } public void sendmail() {/*  try {   String host = "smtp.gmail.com";   String from = "[email protected]";   String to = "@163.com";   String username = "****";   String password = "****";   String body = "This is a test";   String subject = "Email Test";   SmtpAuth sa = new SmtpAuth();   sa.getuserinfo(username, password);   Session session;   MimeMessage message;   Properties props = System.getProperties();   props.setProperty("proxySet", "true");   props.setProperty("socksProxyHost", "43.82.218.50");   props.setProperty("socksProxyPort", "8080");   props.put("mail.smtp.auth", "true");   props.put("mail.smtp.host", host);   session = Session.getInstance(props, sa);   session.setDebug(true);   message = new MimeMessage(session);   message.setFrom(new InternetAddress(from));   message.setRecipient(Message.RecipientType.TO, new InternetAddress(     to));   message.setSubject(subject);   MimeBodyPart mbp = new MimeBodyPart();   mbp.setText(body);   Multipart mp = new MimeMultipart();   mp.addBodyPart(mbp);   message.setContent(mp);   message.setSentDate(new java.util.Date());   Transport transport = session.getTransport("smtp");   transport.connect(host, username, password);   transport.send(message);  } catch (MessagingException e) {   System.out.println("error" + e.getMessage());  } */} class SmtpAuth extends javax.mail.Authenticator {  private String user, password;  public void getuserinfo(String getuser, String getpassword) {   user = getuser;   password = getpassword;  }  protected javax.mail.PasswordAuthentication getPasswordAuthentication() {   return new javax.mail.PasswordAuthentication(user, password);  } }}

依賴的jar包:http://download.csdn.net/detail/wu858773457/8699111

java發送email(含代理方式,ssl方式,傳統方式)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.