import java.io.File;import java.util.Properties;
import javax.mail.Address;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;
import org.apache.commons.mail.EmailAttachment;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.MultiPartEmail;
public class SendMailByJavaMail {
public static void main(String[] args) throws Exception { setMail1(); }
public static void setMail1() throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp");// props.setProperty("mail.host", "smtp.163.com"); Session session = Session.getInstance(props); session.setDebug(true);// session.setPasswordAuthentication(url, pw) Message message = new MimeMessage(session); message.setSubject("JAVAMAIL发送测试测试"); message.setFrom(new InternetAddress("testqzmail@163.com")); message.setText("邮件测试");// message.setRecipient(Message.RecipientType.TO, new InternetAddress("hzy5202008@163.com")); Transport transport = session.getTransport(); transport.connect("smtp.163.com", 25, "testqzmail", "mima"); transport.sendMessage(message, new Address[] {new InternetAddress("mima@qq.com")}); transport.close(); }
public static void setMail2() throws Exception { Properties props = System.getProperties(); props.setProperty("mail.smtp.auth", "true");// props.setProperty("mail.host", "192.168.1.135"); props.setProperty("mail.host", "smtp.163.com"); Session session = Session.getInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("testqzmail", "mima"); } }); session.setDebug(true); Message message = new MimeMessage(session); message.setSubject("hello");// message.setRecipient(Message.RecipientType.TO, new InternetAddress("lq@192.168.1.135")); message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("mima@qq.com")); message.setFrom(new InternetAddress("testqzmail@163.com"));// message.setText("aaaaa"); message.setContent("<span style='color:red'>hello 中国</span>","text/html;charset=gbk"); Transport.send(message); } public static void setMail3() throws Exception {// SimpleEmail email = new SimpleEmail();//如果发送普通的邮件,使用这个类就可以了 MultiPartEmail email = new MultiPartEmail();//如果要发送带附件的邮件,需使用这个类// HtmlEmail email = new HtmlEmail();//可以发送html类型的邮件 email.setHostName("smtp.163.com");//指定要使用的邮件服务器 email.setAuthentication("testqzmail", "mima");//使用163的邮件服务器需提供在163已注册的用户名、密码 email.setCharset("UTF-8"); try { email.setFrom("testqzmail@163.com");//设置发件人 email.addTo("mima@qq.com");//设置收件人 email.setSubject("测试邮件");//设置主题 email.setMsg("测试邮件测试邮件测试邮件");//设置邮件内容 File file = new File("C://test.txt");//要发送的附件 EmailAttachment attachment = new EmailAttachment(); attachment.setPath(file.getPath()); attachment.setName(file.getName()); attachment.setDescription("附件描述"); attachment.setDisposition(EmailAttachment.ATTACHMENT);//附件的类型 email.attach(attachment); email.send(); System.out.println("发送成功"); } catch (EmailException e) { e.printStackTrace(); } } }