当前位置: 技术问答>java相关
smtp的问题
来源: 互联网 发布时间:2015-01-06
本文导语: 我在用javamail发信时,老是出现SMTP provider错误,我看了前面的文章,设为smtp.126.com还是没用,怎么回事?(在linux下) | package testthread; import java.util.*; import java.io.*; import javax.mail.*; import java...
我在用javamail发信时,老是出现SMTP provider错误,我看了前面的文章,设为smtp.126.com还是没用,怎么回事?(在linux下)
|
package testthread;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class testthread{
//定义收件人、发送人、主题等
String to="",from="",host="",filename="",messagetext="",subject="";
boolean debug=false;
//保存发送的文件名
Vector vfile = new Vector(10,10);
//保存发送的正文
Vector vmsg = new Vector(10,10);
public testthread() {}
public testthread(String to,String from,String smtpServer,String subject)
{
//初始化收件人、发送人、主题
this.to=to;
this.from=from;
this.host=smtpServer;
this.subject=subject;
}
//收集附加文件
public void attachfile(String fname)
{
vfile.addElement(fname);
}
//收集邮件正文
public void setMessage(String msg)
{
vmsg.addElement(msg);
}
//设置调试标志
public void setDebug(boolean debug)
{
this.debug=debug;
}
//邮件发送函数
public boolean startSend()
{
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session=Session.getDefaultInstance
(props, null);
session.setDebug(debug);
try {
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//把message part加入新创建的Multipart
Multipart mp = new MimeMultipart();
// 邮件内容的第一部分
Enumeration emsg=vmsg.elements();
while(emsg.hasMoreElements())
{
messagetext=emsg.nextElement().toString();
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(messagetext);
mp.addBodyPart(mbp1);
}
vmsg.removeAllElements();
// 邮件内容的第二部分
Enumeration efile=vfile.elements();
while(efile.hasMoreElements())
{
MimeBodyPart mbp2=new MimeBodyPart();
filename=efile .nextElement().toString();
FileDataSource fds=new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}
vfile.removeAllElements();
// 把MultiPart加入邮件
msg.setContent(mp);
// 设置邮件头的发送日期
msg.setSentDate(new Date());
// 发送邮件
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null)
{
ex.printStackTrace();
}
return false;
}
return true;
}//end public void startSend()
public static void main(String[] args)
{
testthread sendmail=new testthread("tinyjack@263.net","cflames@21cn.com","smtp.21cn.com","test JavaMail API");
sendmail.setMessage("send a file");
sendmail.setMessage("send a javafile");
sendmail.startSend();
}
}
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class testthread{
//定义收件人、发送人、主题等
String to="",from="",host="",filename="",messagetext="",subject="";
boolean debug=false;
//保存发送的文件名
Vector vfile = new Vector(10,10);
//保存发送的正文
Vector vmsg = new Vector(10,10);
public testthread() {}
public testthread(String to,String from,String smtpServer,String subject)
{
//初始化收件人、发送人、主题
this.to=to;
this.from=from;
this.host=smtpServer;
this.subject=subject;
}
//收集附加文件
public void attachfile(String fname)
{
vfile.addElement(fname);
}
//收集邮件正文
public void setMessage(String msg)
{
vmsg.addElement(msg);
}
//设置调试标志
public void setDebug(boolean debug)
{
this.debug=debug;
}
//邮件发送函数
public boolean startSend()
{
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session=Session.getDefaultInstance
(props, null);
session.setDebug(debug);
try {
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//把message part加入新创建的Multipart
Multipart mp = new MimeMultipart();
// 邮件内容的第一部分
Enumeration emsg=vmsg.elements();
while(emsg.hasMoreElements())
{
messagetext=emsg.nextElement().toString();
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(messagetext);
mp.addBodyPart(mbp1);
}
vmsg.removeAllElements();
// 邮件内容的第二部分
Enumeration efile=vfile.elements();
while(efile.hasMoreElements())
{
MimeBodyPart mbp2=new MimeBodyPart();
filename=efile .nextElement().toString();
FileDataSource fds=new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}
vfile.removeAllElements();
// 把MultiPart加入邮件
msg.setContent(mp);
// 设置邮件头的发送日期
msg.setSentDate(new Date());
// 发送邮件
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null)
{
ex.printStackTrace();
}
return false;
}
return true;
}//end public void startSend()
public static void main(String[] args)
{
testthread sendmail=new testthread("tinyjack@263.net","cflames@21cn.com","smtp.21cn.com","test JavaMail API");
sendmail.setMessage("send a file");
sendmail.setMessage("send a javafile");
sendmail.startSend();
}
}