❶ java 关于邮件发送,接受地址填的是163或者QQ的,但不知道为啥总错误
1.自己电脑不需要配smtp,你是用163的smtp
2.553是收件人地址无法解析的错误,在addRecipients那一行断点,看看toAddr传过来的参数是不是有问题
❷ 急java 发送邮件 急!~急!~ 高手快进来帮忙看看啊!~
这是我的连续发几封邮件的代码。连续发是为了163邮箱的积分,^_^。你可以参考下。
package com.test;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail extends Authenticator {
public static final String mailuser = "xianlaioy"; // 需要你的邮箱帐号(仅@符号前面的)
public static final String password = "######"; // 需要你的邮箱密码
private Properties props;
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailuser, password);
}
public SendEmail(){
props = new Properties();
InputStream is = null;
try {
/*当部署在web容器中时,路径要修改
* Thread.currentThread().getContextClassLoader().getResource("").getPath();
* 得到的url是web容器中的项目下的class路径
**/
is = new FileInputStream("src/sendemail.properties");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
props.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
/** email为接收邮箱,text为邮件正文
* @throws IOException */
public void sendEamils(String email, String text) throws IOException {
Transport transport = null;
try {
props.setProperty("mail.smtp.host", props.getProperty("mail.smtp.host"));
props.setProperty("mail.smtp.auth", props.getProperty("mail.smtp.auth"));
SendEmail auth = new SendEmail();
javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,
auth);
MimeMessage message = new MimeMessage(session);
Address addressFrom = new InternetAddress(SendEmail.mailuser
+ "@163.com", "欧阳");
Address addressTo = new InternetAddress(email, "xianlai");
message.setText(text);
message.setSubject("邮件标题");
message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO, addressTo);
message.saveChanges();
transport = session.getTransport("smtp");
session.setDebug(true);
/*transport.connect("smtp.126.com", SendEmail.mailuser,
SendEmail.password);*/
transport.send(message);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Timer t = new Timer();
long delay =2*60*1000;
t.schele(new TimerTask(){
private int count=0;
private SendEmail se=new SendEmail();//作为成员变量就只会创建一个SendEmail对象。
public void run() {
//在timer的run中就必须考虑好怎样或者什么时候退出的问题。
//这里是用count计数的方法来考虑退出的问题的。
if(count++<1){
int i=10;
for (int j = 0; j < i; j++) {
try {
se.sendEamils("[email protected]","test"+j );
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
this.cancel();
System.exit(0);
}
}}, 2000,delay);
}
}