如果,出现连接正常,但,发送报错。检查是不是以下包没有导入lib中,
示例代码
发送用sina邮箱,在sina邮箱中要设置开放pop和smtp功能
Send.java
package com.Email;
import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class Send {
String SMTPHost="";
String user="";
String password="";
String from="";
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
String to="";
String subject="";
String content="";
//发送邮件
public boolean send(){
Properties props=new Properties(); //
props.put("mail.smtp.host", this.SMTPHost);
props.put("mail.smtp.auth", "true");
try {
SmtpAuth auth=new SmtpAuth();
auth.setAccount(user, password);
Session mailSession=Session.getDefaultInstance(props, auth);
mailSession.setDebug(true);
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject(this.subject);
message.setText(this.content);
message.setSentDate(new Date());
message.setHeader("X-Priority", "1");
message.saveChanges();
Transport transport=mailSession.getTransport("smtp");
transport.connect(this.SMTPHost, user, password);
Transport.send(message,message.getAllRecipients());
transport.close();
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
static class SmtpAuth extends Authenticator{
String user,password;
void setAccount(String user,String password){
this.user=user;
this.password=password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
}
// /**
// * @param args
// */
// public static void main(String[] args) {
// // TODO Auto-generated method stub
//
// }
public String getSMTPHost() {
return SMTPHost;
}
public void setSMTPHost(String sMTPHost) {
SMTPHost = sMTPHost;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
try {
content=new String(content.getBytes("iso8859-1"),"utf-8");
} catch (Exception e) {
// TODO: handle exception
}
this.content = content;
}
}
textMail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'textMail.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>发送文本邮件</h2>
<form name="form1" method="post" action="sendMail1.jsp">
SMTP服务器:<input type="text" id="SMTPHost" name="SMTPHost"/>
<br>
登录账号:<input type="text" id="user" name="user"/>
<br>
登录密码:<input type="password" id="password" name="password"/>
<br>
发件人邮箱:<input type="text" id="from" name="from"/>
<br>
收件人邮箱:<input type="text" id="to" name="to"/>
<br>
邮件标题:<input type="text" id="subject" name="subject"/>
<br>
邮件内容:<input type="text" id="content" name="content"/>
<br>
<input type="submit" value="发达">
</form>
</body>
</html>
sendMail1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:useBean id="mySend" class="com.Email.Send"></jsp:useBean>
<jsp:setProperty property="*" name="mySend"/>
<body>
<%
boolean status=mySend.send();
if(status){
out.print("成功");
}else{
out.print("失败");
}
%>