JavaMail保存为草稿邮件(只适合imap不适合pop3)

45 篇文章 1 订阅

               JavaMail保存为草稿邮件(只适合imap不适合pop3)

 

转载:https://blog.csdn.net/u013183865/article/details/40507145?locationNum=1&fps=1
 


 
import java.util.Date;
import java.util.Properties;
 
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
 
public class SaveDraf {
	
	
	
	public static String host = "******"; // 发件服务器
	public static String username = "*****";//邮箱账户
	public static String password = "****";//密码
	
	public static void main(String[] args) {
		sendMail("收件邮箱", "邮件主题", "<h1>呜哈哈哈哈</h1>");
	}
	
	
	/**
	 * @param to 收件人
	 * @param title 主题
	 * @param content 内容
	 */
	public static void sendMail(String to, String title, String content) {
		Properties props = System.getProperties();
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", "25");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
		try {		
			Properties prop = new Properties();
			Session session1 = Session.getDefaultInstance(prop, null);
			Store store = session1.getStore("imap");
			store.connect(host, username, password);
			Folder folder = store.getFolder("Drafts");// 打开草稿箱	
			MimeMessage mmessage = new MimeMessage(session);
			mmessage.setFrom(new InternetAddress(username));
			mmessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
			mmessage.setSubject(title);
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(content, "text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			mmessage.setContent(mainPart);
			mmessage.setSentDate(new Date());
			mmessage.saveChanges();
			mmessage.setFlag(Flags.Flag.DRAFT, true);
			MimeMessage draftMessages[] = {mmessage};
			System.out.println(mmessage.getSubject());
			folder.appendMessages(draftMessages);
//			Transport.send(mmessage);
			System.out.println("保存成功");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

 

  • 1
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值