为了账号安全,请及时绑定邮箱和手机立即绑定

您好,以下是关于javamail的错误,加上send就出错,求助解决方法!

您好,以下是关于javamail的错误,加上send就出错,求助解决方法!

慕桂英4014372 2021-11-11 11:07:23
package cn.hxex.library.service.impl;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.JavaMailSenderImpl;import cn.hxex.library.model.Email;import cn.hxex.library.service.EmailService;public class EmailServiceImpl implements EmailService{public Email sendEmail(String subject, String object){JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();// 设定 Mail ServersenderImpl.setHost("smtp.qq.com");//SMTP验证时,需要用户名和密码senderImpl.setUsername("####");senderImpl.setPassword("####");// 建立邮件讯息SimpleMailMessage mailMessage = new SimpleMailMessage();// 设定收件人、寄件人、主题与内文mailMessage.setTo("####@qq.com");mailMessage.setFrom("####@qq.com");mailMessage.setSubject(subject);mailMessage.setText(object);// 传送邮件//senderImpl.send(mailMessage);就是这句一加上就错了!!!System.out.println("邮件传送OK..");return null;}}
查看完整描述

1 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

send(msg):是一个静态方法,需要用Transport类直接调用,Transport.send(msg);
或者用Transport的一个实例transport:
transport.connect(host,port,username,password)
transport.sendMessage(msg,mailTo)
transport.close();
建议你全部贴出来看一下啦!我用下面代码可以发出来的!(邮件地址你改一下就可以了)
Properties props = new Properties();
props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.sina.com");
Session session =Session.getInstance(props, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourmail@sina.com","yourpassword");
}
});
Message msg = new MimeMessage(session);
msg.setSubject("subject ");
msg.setText("mailtext!");
Address from = new InternetAddress("yourmail@sina.com");//发送地址
Address to = new InternetAddress("mail1@yeah.net");//接收地址
Address cc = new InternetAddress("mail2@163.com");//抄送地址
Address bcc = new InternetAddress("mail3@126.com");//密送地址
msg.setFrom(from);
msg.setRecipient(RecipientType.TO, to);
msg.setRecipient(RecipientType.CC, cc);
msg.setRecipient(RecipientType.BCC, bcc);

Transport.send(msg);//这个直接包含了打开连接/发送邮件/关闭连接



查看完整回答
反对 回复 2021-11-14
  • 1 回答
  • 0 关注
  • 304 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信