Java论坛网»Java技术»自动发送电子邮件程序
自动发送电子邮件程序
问?:
为什么只能发公司的邮件成功,发给其他邮件系统就不成功?
package javamail;
/**
* @author Administrator
*
*/
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
//import UploadFile.*;
import org.apache.log4j.*;
public class AutoSendMail {
public AutoSendMail() {
}
// public Log4J log4j = new Log4J();
String host = null;
String username = null;
String password = null;
public void setHost(String host) {
this.host = host;}
public void setAccount(String username, String password) {
this.username = username;
this.password = password;
}
public void AutoSendMail(String from, String to, String cc, String subject,
String content) {
Properties props = new Properties();
props.put("mail.smtp.host", "aa.test.com.cn");
//props.put("mail.smtp.auth", "true");
try {
Session mailSession = Session.getDefaultInstance(props,null);
mailSession.setDebug(false);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(
cc));
message.setSubject(subject);
message.setHeader("Dear All", "");
message.setText(content);
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception ex) {
// log4j.error.error("Auto Send Mail Error : " + ex.getMessage());
System.out.println(ex.getMessage());
}
}
public void AutoSendMail(String from, String to, String subject,
String content) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
//props.put("mail.smtp.auth","true");
//props.put("mail.smtp.auth", "true");
try {
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(false);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
message.setHeader("Dear All", "");
message.setText(content);
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception ex) {
// log4j.error.error("Auto Send Mail Error : " + ex.getMessage());
System.out.println(ex.getMessage());
}
}
public static void main(String args[]){
AutoSendMail send = new AutoSendMail();
}
}
package javamail;
/**
* @author Administrator
*
*/
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
//import UploadFile.*;
import org.apache.log4j.*;
public class AutoSendMail {
public AutoSendMail() {
}
// public Log4J log4j = new Log4J();
String host = null;
String username = null;
String password = null;
public void setHost(String host) {
this.host = host;}
public void setAccount(String username, String password) {
this.username = username;
this.password = password;
}
public void AutoSendMail(String from, String to, String cc, String subject,
String content) {
Properties props = new Properties();
props.put("mail.smtp.host", "aa.test.com.cn");
//props.put("mail.smtp.auth", "true");
try {
Session mailSession = Session.getDefaultInstance(props,null);
mailSession.setDebug(false);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(
cc));
message.setSubject(subject);
message.setHeader("Dear All", "");
message.setText(content);
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception ex) {
// log4j.error.error("Auto Send Mail Error : " + ex.getMessage());
System.out.println(ex.getMessage());
}
}
public void AutoSendMail(String from, String to, String subject,
String content) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
//props.put("mail.smtp.auth","true");
//props.put("mail.smtp.auth", "true");
try {
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(false);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
message.setHeader("Dear All", "");
message.setText(content);
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception ex) {
// log4j.error.error("Auto Send Mail Error : " + ex.getMessage());
System.out.println(ex.getMessage());
}
}
public static void main(String args[]){
AutoSendMail send = new AutoSendMail();
}
}
答!: 1:
把身份验证带上啊
答!: 2:
身份验证带上好象也不行呢,不知那还有比较好的例子
相关JAVA教程:
这是什么问题啊,弄了我几天了,我老是得不到 session
8月31日CSDNF2F俱乐部活动—与夏昕、林信良先生在线讨论“轻量级开发”
Sun网页上的RMI Activation范例谁调通了?
求:<<深入浅出Hibernate>> 源码
求substance的教程
webwork+hibernate多表内联查询的显示!!!!!!11
tomcat 中文问题
java程序中如何调用javascript函数?
在java中如何调用javascript函数啊?
关于使用swing简单问题
一个把我卡住的问题,关于图形界面的。
請教大蝦,如何用jacob將office 2007中的文件打開,另存