当前页面: 开发资料首页 → J2EE 专题 → 用spring发送电子邮件的问题
用spring发送电子邮件的问题
摘要: 用spring发送电子邮件的问题
我的代码是:
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.SimpleMailMessage;
public class SimpleMailDemo {
public static void main(String[] args) throws Exception {
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定 Mail Server
senderImpl.setHost("smtp.sohu.net");
// 建立邮件讯息
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设定收件人、寄件人、主题与内文
mailMessage.setTo("inter0507@sohu.com");
mailMessage.setFrom("kaka@sina.com");
mailMessage.setSubject("Test");
mailMessage.setText("This is a test!!!dffffffffffffffffffffff");
// 传送邮件
senderImpl.send(mailMessage);
System.out.println("邮件传送OK..");
}
}
但是除了问题:
Exception in thread "main" org.springframework.mail.MailSendException: Could not send mails: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 554
: Recipient address rejected: Relay access denied #1-062
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 554 : Recipient address rejected: Relay access denied #1-062
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1130)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:525)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:382)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)
at onlyfun.caterpillar.SimpleMailDemo.main(SimpleMailDemo.java:22)
那位兄弟能帮我解决一下,感谢
这个我自己用过,好使
public static void sendMail()
{
System.out.println("dkkdkdkkdk");
JavaMailSenderImpl ms = new JavaMailSenderImpl();
ms.setHost("smtp.sina.com.cn");
ms.setUsername("kkkkk@sina.com");
ms.setPassword("kkkkkkk");
Properties pt = new Properties();
pt.setProperty("mail.smtp.auth","true");
ms.setJavaMailProperties(pt);
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom("kkkkkk@sina.com");
msg.setTo("kkkkk@hotmail.com");
msg.setSubject("congratulate you!");
msg.setText("hello,you are success! great!");
msg.setCc("kkkkkk@hotmail.com");
try{
ms.send(msg);
System.out.println("ok");
}catch(Exception e)
{
e.printStackTrace();
}
}
你照着看看