form提交到邮箱

AwL_1124 2008-08-02 04:30:17
RT 通过一个Form把表单的内容提交到一个指定的邮箱里边去
这个是我的一个思路
谁能给个详细的过程实现
谢了·
...全文
295 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
南南北北 2008-08-02
  • 打赏
  • 举报
回复

import java.util.Date;
import java.util.Properties;

import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.mail.smtp.SMTPTransport;

public final class EmailHandle {
//log处理
protected static Log log = LogFactory.getLog(EmailHandle.class);

private String serverIP = ""; //邮件服务器IP
private String serverPort = ""; //邮件服务器端口
private String user = ""; //邮件服务器用户
private String password = ""; //邮件服务器密码

private boolean ssl = false;

private Session session =null;

public EmailHandle(String serverIp,String serverPort,String user,String password,boolean ssl) {
this.serverIP = serverIp;
this.serverPort = serverPort;
this.user = user;
this.password = password;

this.ssl = ssl;
}

/**
* 开始邮件会话
*/
public void beginMailSession() {
if (session==null) {

Properties props = System.getProperties();

props.put("mail.smtp.host", serverIP);
props.put("mail.smtp.port", serverPort);
props.put("mail.smtp.auth", "true");

session = Session.getInstance(props, new MyAuthenticator(user,password));

}
}

/**
* 发送文本邮件
*
* @param to
* @param subject
* @param from
* @param cc //可空
* @param bcc //可空
* @param body
* @throws AddressException
* @throws MessagingException
*/
public void sentTextMail(String to,String subject,String from,String cc,String bcc,String body) throws AddressException, MessagingException {
if (session!=null) {

Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();

msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));

msg.setSubject(subject);
msg.setText(body);

msg.setHeader("X-Mailer", "邮件头");
msg.setSentDate(new Date());

SMTPTransport t =
(SMTPTransport)session.getTransport(ssl ? "smtps" : "smtp");
try {
if (user!=null && password!=null)
t.connect(serverIP, user, password);
else
t.connect();
t.sendMessage(msg, msg.getAllRecipients());
}catch(AuthenticationFailedException e){
log.error(e, e.getCause());
}
catch(MessagingException e){
log.error(e, e.getCause());
}
finally {
t.close();
}

}
}

/**
* 邮件授权认证类
*
*/
private class MyAuthenticator extends javax.mail.Authenticator{

private String userName;
private String password;

public MyAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,password);
}

}

}


在servlet中调用:

EmailHandle handle = new EmailHandle(mailServerIp,mailServerPort,mailUser,mailPassword,false);
handle.beginMailSession();
handle.sentTextMail(email1, "邮件主题", mailUser,null,null,"其他内容");
qlrhoo 2008-08-02
  • 打赏
  • 举报
回复
设置配置文件xml
然后通过调用参数的方法应用其邮箱地址

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧