求java实现邮件发送的源代码

发布网友 发布时间:2022-04-21 15:35

我来回答

2个回答

热心网友 时间:2023-07-30 13:24

import java.util.*;
import javax.mail.*;import javax.mail.internet.*;
public class JMail {
public void SendMail(String Topic,String Content){Properties props=new Properties();props.put("mail.smtp.host","smtp.163.com");props.put("mail.smtp.auth","true");Session s=Session.getInstance(props);s.setDebug(false);MimeMessage message=new MimeMessage(s);MimeMultipart mp=new MimeMultipart();BodyPart body = new MimeBodyPart();InternetAddress from;InternetAddress to;try{from=new InternetAddress("发件人邮箱");message.setFrom(from);to = new InternetAddress("收件人邮箱");message.setRecipient(Message.RecipientType.TO,to);message.setSubject(Topic,"utf-8"); body.setContent(Content, "text/html;charset=utf-8"); mp.addBodyPart(body); message.setContent(mp); message.setSentDate(new Date());message.saveChanges();Transport transport=s.getTransport("smtp");transport.connect("smtp.163.com(邮件服务商,这是163的)","发件邮箱","发件邮箱密码");transport.sendMessage(message,message.getAllRecipients());transport.close();}catch(AddressException e){e.printStackTrace();}catch(MessagingException e){e.printStackTrace();}}}

热心网友 时间:2023-07-30 13:25

需要用到javamail的jar包,网上有。找不到把邮箱贴出来,我发给你。 package test.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.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SendMailServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2960000940515881314L;
private ServletConfig config = null;
private static final String CONTENT_TYPE = "text/html"; public void init(ServletConfig config1) throws ServletException {
this.config = config1;
} final public ServletConfig getServletConfig() {
return config;
} /**
* Constructor of the object.
*/
public SendMailServlet() {
super();
} /**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy();
} /**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String to_mail=codeToString(request.getParameter("to"));
String to_title=codeToString(request.getParameter("title"));
String to_content=codeToString(request.getParameter("content"));

Properties props=new Properties();//也可用Properties props = System.getProperties(); //
props.put("mail.smtp.host","localhost");//存储发送邮件服务器的信息
props.put("mail.smtp.auth","true");//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true); //由邮件会话新建一个消息对象
MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象 //设置邮件
InternetAddress from;
try {
from = new InternetAddress("06254054@bjtu.mail.com");
message.setFrom(from);//设置发件人
InternetAddress to=new InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(to_title);//设置主题
message.setText(to_content);//设置信件内容
message.setSentDate(new Date());//设置发信时间
//发送邮件
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport("smtp");
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect("localhost",8479,"","");
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close();
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.print("success");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} }

public String codeToString(String str) {//处理中文字符串的函数
String s=str;
try {
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
} catch(Exception e) {
return s;
}
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com