javaMail发送邮件时遇到的问题

发送邮箱代码块

	/**
	 * 发送邮件
	 * 
	 * @param mailInfo
	 * 邮件信息
	 * @throws Exception
	 */
	public boolean sendHtmlMail(MailSenderInfo mailInfo) throws Exception {
		// 判断是否需要身份认证
		MyAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();
                //设置协议  基本这里应该是最容易出问题的地方好像,一些网易邮箱的504错误还有QQ邮箱535、530等错误都是因为这里没有设置好
		pro.setProperty("mail.transport.protocol", "smtp"); 
		pro.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		pro.setProperty("mail.smtp.socketFactory.fallback", "false");
		pro.setProperty("mail.smtp.port", "465");
		pro.setProperty("mail.smtp.socketFactory.port", "465");
		pro.setProperty("mail.smtp.auth", "true");
		// 如果需要身份认证,则创建一个密码验证器
		if (mailInfo.isValidate()) {
			authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		
		// 发送者地址
		Address from = new InternetAddress(mailInfo.getFromAddress());
		// 发送者
		mailMessage.setFrom(from);
		//接收者地址
		Address to = new InternetAddress(mailInfo.getToAddress());
		mailMessage.setRecipient(Message.RecipientType.TO, to);
		// 设置邮件消息的主题
		mailMessage.setSubject(mailInfo.getSubject());
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(mainPart);
		// 发送邮件
		Transport.send(mailMessage);
		return true;
	}

163为发送邮箱

public static void main(String[] args) throws Exception {
		 MailSenderInfo mailInfo = new MailSenderInfo();
		 mailInfo.setMailServerHost("smtp.163.com");
		 mailInfo.setUserName("******@163.com");
		 mailInfo.setPassword("****");// 发送方邮箱密码,不填密码,填校验码,登陆邮箱设置,在下图
		 mailInfo.setFromAddress("******@163.com");
		
		 mailInfo.setToAddress("******@qq.com");
		 mailInfo.setSubject("邮箱标题");
		 mailInfo.setContent("邮箱内容啦啦啦啦啦啦");
		 SimpleMailSender sms = new SimpleMailSender();
		 sms.sendHtmlMail(mailInfo);

	}

qq邮箱为发送邮箱

public static void main(String[] args) throws Exception {
		 MailSenderInfo mailInfo = new MailSenderInfo();
		 mailInfo.setMailServerHost("smtp.qq.com");
		 mailInfo.setMailServerPort("465");
		 mailInfo.setUserName("******@qq.com");
		 mailInfo.setPassword("ogx******");// 您的邮箱密码,也不是密码,要到qq邮箱设置,下图
		 mailInfo.setFromAddress("1*******@qq.com");
		
                    mailInfo.setToAddress("********@163.com");		
                 mailInfo.setSubject("邮箱标题");
	        mailInfo.setContent("邮箱内容啦啦啦啦啦啦");
	        SimpleMailSender sms = new SimpleMailSender();
	        sms.sendHtmlMail(mailInfo);
}

开启这些服务,按提示发个短信就可以了,用得到的授权码代替密码,不然会报错,因为这里设置了所以上面ssl配置一定要配置好咯,一般好像只开启第一个就可以了,但是在不断调试的过程中,成功的那一刻这里是两个都开启了,所以不知道只开启一个可不可以~


另外,有说163邮箱发送报554的错误,可以参考这个,抄送给自己,试过,可以

也有说Transport.send(mailMessage);这个方法不行的,换成

                    Transport transport;
	            //创建一个发送器transport用来发送邮件内容  
	            transport = sendMailSession.getTransport();  
	            transport.connect("smtp.qq.com", 465, "******@qq.com", "ogx*******");//qq发送,端口465,发送者邮箱,授权码  
	            //后面的邮箱是收件人的邮箱,可以指定多个,赋值用下面注释这个方法
                    //mailMessage.addRecipients(Message.RecipientType.TO,InternetAddress.parse(mailInfo.getToAddress()));//目的邮箱
	            transport.sendMessage(mailMessage, mailMessage.getAllRecipients()); 
	            transport.close();  
还有报这个错的:javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.......port 25好像不能用25这个端口吧,改成465。(之前就是因为这个错误开始改的,然后一系列的其他的错一个一个消灭!)

有参考这篇https://bbs.csdn.net/topics/391959900博客11楼的回答

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值