当前位置: 技术问答>java相关
100分求助:一个最简单的javamail中出现的问题
来源: 互联网 发布时间:2015-09-08
本文导语: 源代码如下: import java.io.*; import java.net.InetAddress; import java.util.Properties; import java.util.Date; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; /** * 这个例子说明了一个简单的 email 发送程序 * * Titl...
源代码如下:
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* 这个例子说明了一个简单的 email 发送程序
*
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class Email1 {
public static void main(String[] argv) {
new Email1(argv);
}
public Email1(String[] argv) {
//收件人
String to = null;
//发件人
String from = null;
//主题
String subject = null;
//抄送人
String cc = null;
//暗抄送
String bcc = null;
// mail 主机
String mailhost = null;
// mail 内容
String content = null;
//MIME邮件对象
MimeMessage mimeMsg = null;
//邮件会话对象
Session session = null;
try {
mailhost = "wangshuai";
from = "from@wangshuai";
to = "to@wangshuai";
subject = "您好";
content = "最简单的 JavaMail";
Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
//获得邮件会话对象
session = Session.getDefaultInstance(props,null);
//创建MIME邮件对象
mimeMsg = new MimeMessage( session );
//设置发信人
mimeMsg.setFrom(new InternetAddress( from ) );
//设置收信人
if(to!=null){
mimeMsg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
}
//设置抄送人
if(cc!=null){
mimeMsg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc ) );
}
//设置暗送人
if(bcc!=null){
mimeMsg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc ) );
}
//设置邮件主题
mimeMsg.setSubject(subject,"GBK");
//设置邮件内容
mimeMsg.setText( content ,"GBK" );
//发送日期
mimeMsg.setSentDate(new Date());
//发送邮件
Transport.send( mimeMsg );
System.out.println( "email send!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加到工程里以后,出现错误提示如下给怎么解决:
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Unknown SMTP host: wangshuai;
nested exception is:
java.net.UnknownHostException: wangshuai
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at sendmail.Email1.(Email1.java:88)
at sendmail.Email1.main(Email1.java:25)
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* 这个例子说明了一个简单的 email 发送程序
*
*
Title:
*
Description:
*
Copyright: Copyright (c) 2001
*
Company:
* @author unascribed
* @version 1.0
*/
public class Email1 {
public static void main(String[] argv) {
new Email1(argv);
}
public Email1(String[] argv) {
//收件人
String to = null;
//发件人
String from = null;
//主题
String subject = null;
//抄送人
String cc = null;
//暗抄送
String bcc = null;
// mail 主机
String mailhost = null;
// mail 内容
String content = null;
//MIME邮件对象
MimeMessage mimeMsg = null;
//邮件会话对象
Session session = null;
try {
mailhost = "wangshuai";
from = "from@wangshuai";
to = "to@wangshuai";
subject = "您好";
content = "最简单的 JavaMail";
Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
//获得邮件会话对象
session = Session.getDefaultInstance(props,null);
//创建MIME邮件对象
mimeMsg = new MimeMessage( session );
//设置发信人
mimeMsg.setFrom(new InternetAddress( from ) );
//设置收信人
if(to!=null){
mimeMsg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
}
//设置抄送人
if(cc!=null){
mimeMsg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc ) );
}
//设置暗送人
if(bcc!=null){
mimeMsg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc ) );
}
//设置邮件主题
mimeMsg.setSubject(subject,"GBK");
//设置邮件内容
mimeMsg.setText( content ,"GBK" );
//发送日期
mimeMsg.setSentDate(new Date());
//发送邮件
Transport.send( mimeMsg );
System.out.println( "email send!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加到工程里以后,出现错误提示如下给怎么解决:
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Unknown SMTP host: wangshuai;
nested exception is:
java.net.UnknownHostException: wangshuai
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at sendmail.Email1.(Email1.java:88)
at sendmail.Email1.main(Email1.java:25)
|
Properties props=System.getProperties();
props.setProperty("mail.transport.protocol","smtp"); //smtp协?
props.setProperty("mail.smtp.host",m_host); //服务器地址
props.setProperty("mail.smtp.port",""+m_port); //端口号
发送邮件部分:
Transport trans=sess.getTransport();
trans.connect(m_host,m_port,m_user,m_pass);
trans.sendMessage(msg,m_to);
props.setProperty("mail.transport.protocol","smtp"); //smtp协?
props.setProperty("mail.smtp.host",m_host); //服务器地址
props.setProperty("mail.smtp.port",""+m_port); //端口号
发送邮件部分:
Transport trans=sess.getTransport();
trans.connect(m_host,m_port,m_user,m_pass);
trans.sendMessage(msg,m_to);