借用163服务测试发送邮件类

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();
  }
 }
 
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值