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

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 Server
senderImpl.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;
}

}

慕桂英4014372
浏览 305回答 1
1回答

慕的地6264312

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(){@Overrideprotected 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);//这个直接包含了打开连接/发送邮件/关闭连接
打开App,查看更多内容
随时随地看视频慕课网APP