❶ 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);
}
}