关于Jmail不能发送邮件问题

如果,出现连接正常,但,发送报错。检查是不是以下包没有导入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("失败");
   }
  
    %>

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘怀帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值