再发一次, 使用Java Mail时发送html格式的邮件结果内容为空???求高手解答啊。

三国vip 发布于 2013/03/10 18:33
阅读 1K+
收藏 1

上次可能没把问题说清楚,因而小弟再发一次,求高手解答。

使用Eclipse开发一个功能需要用到邮箱验证。单独运行为Java Application时,发送一切正常。但是转为Java Web项目后,项目使用Tomcat作为服务器,结果在自己编写的代码中发送Html格式的邮件时接收到的邮件内容始终为空,但是如果发送文本格式邮件时,内容正常显示。我是这么做的:

1、jsp页面提交邮箱,代码如下:

<form method="post" action="EmailServlet?service=findPwd" id="findpwdform" name="findpwdform">
<table>
<tr>
<td>邮箱:</td>
<td>
<input name="email_address"id="email_address"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="submit" value="提交" onclick="emailCheck()">
</td>
</tr>
</table>
</form>

2、servlet进行处理,调用发送邮件的方法Send(),servlet代码如下:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
String service = req.getParameter("service");


if ("findPwd".equals(service)) {
String email = req.getParameter("email_address");
String content = ". To reset your password, click the URL below. " +
"<a href='http://10.17.21.214:8400/test/"
+ "REGISTERSERVLET?SERVICE=PASSWORD_RESETS&GUID="
+ "'>http://10.17.21.214:8400/test/password_resets/"
+ "</a>"
+ "If you did not request your password to be reset, just ignore "
+ "this email and your password will continue to stay the same.";
SendMail.Send("Password Reset", email, content);
}

3、发送邮件的方法Send(),代码如下:

public static void Send(String subject,String toEmail,String cont){
//建立会话
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.transport.procotol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true);

//建立邮件内容
MimeMessage msg = new MimeMessage(session);
MimeMultipart msgMultipart = new MimeMultipart("mixed");
try {
msg.setContent(msgMultipart);


MimeBodyPart content = new MimeBodyPart();
msgMultipart.addBodyPart(content);
MimeMultipart bodyMultipart = new MimeMultipart("related");
content.setContent(bodyMultipart);
MimeBodyPart htmlPart = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlPart);


//设置html内容
htmlPart.setContent(cont,"text/html;charset=utf-8");

//设置发件人
msg.setFrom(new InternetAddress("sdustxinxi2009@163.com"));
//设置标题
msg.setSubject(subject);

Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", 25, "sdustxinxi2009@163.com", "13-120");
transport.sendMessage(msg, new InternetAddress[]{new InternetAddress(toEmail)});
transport.close();
} catch (Exception e) {
e.printStackTrace();
}

加载中
0
彭博
彭博

兄弟,露底裤了。代码里面的帐号密码如果有用还是改一下吧

三国vip
三国vip
我知道,那是公邮,嘿嘿,不用改
0
彭博
彭博
N久前写的一个用Gmail发邮件的:
package com.campustaste.common;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Gmail 邮件服务
 */
public class GmailService extends Thread{
  private String      account;
  private String      password;
  private String      reciever;
  private String      title;
  private String      content;

  private boolean send(final String account, final String password,
      String reciever, String title, String content) {
    try {
      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
      final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
      // Get a Properties object
      Properties props = System.getProperties();
      props.setProperty("mail.smtp.host", "smtp.gmail.com");
      props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
      props.setProperty("mail.smtp.socketFactory.fallback", "false");
      props.setProperty("mail.smtp.port", "465");
      props.setProperty("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.auth", "true");
      Session session = Session.getDefaultInstance(props,
          new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(account, password);
            }
          });
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(account));
      msg.setRecipients(Message.RecipientType.TO,
          InternetAddress.parse(reciever, false));
      msg.setSubject(title);
      msg.setText(content);
      msg.setSentDate(new Date());
      Transport.send(msg);
      System.out.println("Message sent to " + reciever + ".");
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public GmailService(final String account, final String password,
      String reciever, String title, String content) {
    this.account = account;
    this.password = password;
    this.reciever = reciever;
    this.title = title;
    this.content = content;
  }
  
  public GmailService() {
    this.account = "xxx@gmail.com";
    this.password = "xxxxxx";
  }

  public void defaultAccountSending(String reciever,String title,String content){
    this.reciever = reciever;
    this.title = title;
    this.content = content;
    this.start();
  }

  @Override
  public void run() {
    this.send(account, password, reciever, title, content);
  }
  
  @Deprecated
  public static void main(String[] args) {
    new GmailService().defaultAccountSending("xxxx@xxx.com", "test", "xxxxxx");
  }

}
三国vip
三国vip
我现在的问题不是不能发送,而是在Java Web项目中发送Html格式的邮件内容为空,而在Java Application中发送Html格式的邮件内容为不为空。这是问题的关键,不信你可以把我的代码在项目中运行一下试试。
0
_DeepBlue
_DeepBlue
给你一个参考
public class Mail {

    private String username;  
    private String password;  
    private String from;  
    private String to;
    private String subject;  
    private String body;
    private String host;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	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 getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
    
    
}
public static void sendMail(final Mail mail) {
 Properties props = System.getProperties();
 props.put("mail.smtp.host", mail.getHost());
 props.put("mail.smtp.auth", "true");
 props.put("mail.transport.protocol", "smtp");
 Authenticator authenticator = new Authenticator() {
 public PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(mail.getUsername(), mail.getPassword());
 }
 };
 Session session = Session.getDefaultInstance(props, authenticator);
 MimeMessage msg = new MimeMessage(session);
 try {
 msg.setFrom(new InternetAddress(mail.getFrom()));
 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail.getTo()));
 msg.setSentDate(new Date());
 msg.setSubject(mail.getSubject());
 msg.setContent(mail.getBody(), "text/html;charset=utf-8");
 Transport.send(msg);
 } catch (AddressException e) {
 LOG.warn(e);
 } catch (MessagingException e) {
 LOG.warn(e);
 }
 }

0
ijk0
ijk0
建议debug一下~~
三国vip
三国vip
你们两个学长怎么都在这里啊,要不直接过去问你们吧,我是隔壁的曹波。我现在的问题不是不能发送,而是在Java Web项目中发送Html格式的邮件内容为空,而在Java Application中发送Html格式的邮件内容为不为空。这是问题的关键,不信你可以把我的代码在项目中运行一下试试。
lushuailufei
lushuailufei
飘过
0
三国vip
三国vip
结束此次问答,问题已找到,Eclipse的问题,换做MyEclipse无问题。
0
明月照大江
明月照大江
楼上的都是实名制了~
三国vip
三国vip
实名制了好啊。
0
HostSugar
HostSugar
发邮件 用apache的 commons email 包吧,便捷!
三国vip
三国vip
返回顶部
顶部