当前位置: 技术问答>java相关
用javamail发邮件,邮件服务器名称怎么设置?
来源: 互联网 发布时间:2015-11-17
本文导语: 在用Javamail写邮件发送程序时,要填写一个邮件服务器的名字,请问各位大虾:是不是可以随便设置一个?我的程序出错就在这个地方。不胜感激! | // java mail package http://java.sun.com/pr...
在用Javamail写邮件发送程序时,要填写一个邮件服务器的名字,请问各位大虾:是不是可以随便设置一个?我的程序出错就在这个地方。不胜感激!
|
// java mail package http://java.sun.com/products/javamail/index.html
//java mail depends on http://java.sun.com/products/javabeans/glasgow/jaf.html
//Set your CLASSPATH to include the "mail.jar" file obtained from
// the download, as well as the current directory.
// Assuming you unzipped javamail-1_2.zip in c:download the
// following would work:
// set CLASSPATH=%CLASSPATH%;c:downloadjavamail-1.2mail.jar;.
// Also include the "activation.jar" file that you obtained from
// downloading the JavaBeans Activation Framework, in your CLASSPATH.
// set CLASSPATH=%CLASSPATH%;c:downloadactivationactivation.jar
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.UnsupportedEncodingException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mail extends JFrame
{
JLabel jlSendMail = new JLabel("Send Mail");
public Mail()
{
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(jlSendMail);
jlSendMail.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
try{
String host = "smtp.21cn.com";//"Mail Server in your Company";
String from = "aaaa@21cn.com";//"From Email";
String to = "bbbb@21cn.com";//"To Email";
Properties props = new Properties();
Session session;
//Store store;
Transport transport;
props.put("mail.smtp.host", host);
session = Session.getInstance(props, null);
//session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from,"Ahmad"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to,"Ahmadwa"));
message.setSubject("First");
//message.setSentDate(new Date());
message.setText("Hi Emad" + (char)(10) + "How are you? This is a test mail send by java program"+new Date());
transport = session.getTransport("smtp");
transport.send(message);
System.out.println("Your Mail Send Successfully");
}
catch (MessagingException e) {System.out.println("1)" + e.toString());}
catch (UnsupportedEncodingException e) {System.out.println("2)" + e.toString());}
}
});
this.setSize(600,600);
this.setVisible(true);
}
public static void main(String args[])
{
new Mail();
}
}
//java mail depends on http://java.sun.com/products/javabeans/glasgow/jaf.html
//Set your CLASSPATH to include the "mail.jar" file obtained from
// the download, as well as the current directory.
// Assuming you unzipped javamail-1_2.zip in c:download the
// following would work:
// set CLASSPATH=%CLASSPATH%;c:downloadjavamail-1.2mail.jar;.
// Also include the "activation.jar" file that you obtained from
// downloading the JavaBeans Activation Framework, in your CLASSPATH.
// set CLASSPATH=%CLASSPATH%;c:downloadactivationactivation.jar
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.UnsupportedEncodingException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mail extends JFrame
{
JLabel jlSendMail = new JLabel("Send Mail");
public Mail()
{
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(jlSendMail);
jlSendMail.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
try{
String host = "smtp.21cn.com";//"Mail Server in your Company";
String from = "aaaa@21cn.com";//"From Email";
String to = "bbbb@21cn.com";//"To Email";
Properties props = new Properties();
Session session;
//Store store;
Transport transport;
props.put("mail.smtp.host", host);
session = Session.getInstance(props, null);
//session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from,"Ahmad"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to,"Ahmadwa"));
message.setSubject("First");
//message.setSentDate(new Date());
message.setText("Hi Emad" + (char)(10) + "How are you? This is a test mail send by java program"+new Date());
transport = session.getTransport("smtp");
transport.send(message);
System.out.println("Your Mail Send Successfully");
}
catch (MessagingException e) {System.out.println("1)" + e.toString());}
catch (UnsupportedEncodingException e) {System.out.println("2)" + e.toString());}
}
});
this.setSize(600,600);
this.setVisible(true);
}
public static void main(String args[])
{
new Mail();
}
}