javamail邮件的发送
package tm.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendMail extends HttpServlet {
public SendMail() {
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gb2312");
String from = request.getParameter("from");
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String context = request.getParameter("context");
// 确定要发送的邮件服务器的地址
String mailserver = "a4c4a156b93141d";
// 设置邮件的传输协议
try {
Properties prop = new Properties();
prop.put("mail.smtp.host", mailserver);
prop.put("mail.smtp.auth","true");
// 建立邮件发送的连接
Session session = Session.getDefaultInstance(prop);
session.setDebug(true);
// 创建发送的信息的载体
Message msg = new MimeMessage(session);
// 设置相关的邮件属性
msg.setFrom(new InternetAddress(from));
// 点到点的发送
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(context);
msg.saveChanges();
// 发送
System.out.println("sending....");
Transport transport=session.getTransport("smtp");
if(transport!=null)
{
System.out.println("transport object is not null");
}
//transport.send(msg);
//transport.sendMessage(message, message.getAllRecipients());
transport.connect("a4c4a156b93141d","Admin","111111");
System.out.println("after connect");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("send over");
} catch (Exception e) {
e.printStackTrace();
out.println("send mail fail!");
}
out.print("send ok");
out.flush();
out.close();
}
}
、、、、、、、、、、、、、、、、、、、、、、
********************************
这是我网上找的例子!把测试的用户名等等换成实际的,就可以了!
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
public class SenderWithSMTPVer
{
String host="";
String user="";
String password="";
public void setHost(String host)
{
this.host=host;
}
public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
}
public void send(String from,String to,String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
try
{
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
message.saveChanges();
Transport transport =null;
transport=mailSession.getTransport("smtp");
if(transport==null)
{
System.out.println("999999");
}
//Transport transport = mailSession.getTransport("smtp");
System.out.println("111");
System.out.println(host);
System.out.println(user);
System.out.println(password);
transport.connect(host, user, password);
System.out.println("222");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("333");
transport.close();
}catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
SenderWithSMTPVer sm=new SenderWithSMTPVer();
sm.setHost("smtp.abc.com");
sm.setAccount("abc","abcabc");
sm.send("
[email protected]","
[email protected]","biaoti","neirong");
}
}