標籤:
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方式,傳統方式)