成都创新互联网站制作重庆分公司

发送邮件java代码 java实现邮件发送功能代码

Java发送邮件

JAVA邮件发送的大致过程是这样的的:

10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有潮州免费网站建设让你可以放心的选择与我们合作。

1、构建一个继承自javax.mail.Authenticator的具体类,并重写里面的getPasswordAuthentication()方法。此类是用作登录校验的,以确保你对该邮箱有发送邮件的权利。

2、构建一个properties文件,该文件中存放SMTP服务器地址等参数。

3、通过构建的properties文件和javax.mail.Authenticator具体类来创建一个javax.mail.Session。Session的创建,就相当于登录邮箱一样。剩下的自然就是新建邮件。

4、构建邮件内容,一般是javax.mail.internet.MimeMessage对象,并指定发送人,收信人,主题,内容等等。

5、使用javax.mail.Transport工具类发送邮件。

java实现发送邮件功能

要实现邮件发送功能需要导入包:mail.jar

/*

* Generated by MyEclipse Struts

* Template path: templates/java/JavaClass.vtl

*/

package org.demo.action;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.demo.form.DemoForm;

public class DemoAction extends Action {

private static final String CONTENT_TYPE = "test/html;charset=GB2312";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

DemoForm demoForm = (DemoForm) form;

System.out.println("标题是" + demoForm.getBiaoti());

System.out.println("内容是" + demoForm.getNeirong());

try {

response.setContentType(CONTENT_TYPE);

String smtphost = "smtp.nj.headware.cn"; // 发送邮件服务器

String user = "q0000015369"; // 邮件服务器登录用户名

String password = "Queshuwen26"; // 邮件服务器登录密码

String from = "q0000015369@nj.headware.cn"; //

String to = "c0000016205@nj.headware.cn"; // 收件人邮件地址

String subject = demoForm.getBiaoti(); // 邮件标题

String body = demoForm.getNeirong(); // 邮件内容

Properties props = new Properties();

props.put("mail.smtp.host", smtphost);

props.put("mail.smtp.auth", "true");

Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);

message.setFrom(fromAddress);

InternetAddress toAddress = new InternetAddress(to);

message.addRecipient(Message.RecipientType.TO, toAddress);

message.setSubject(subject);

message.setText(body);

Transport transport = ssn.getTransport("smtp");

transport.connect(smtphost, user, password);

transport.sendMessage(message, message

.getRecipients(Message.RecipientType.TO));

// transport.send(message);

transport.close();

return mapping.findForward("succ");

} catch (Exception e) {

e.printStackTrace();

return mapping.findForward("fail");

}

}

}

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

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(); } }}


新闻名称:发送邮件java代码 java实现邮件发送功能代码
网页网址:http://cxhlcq.com/article/dddjeii.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部