java发邮件java 发送邮件
java发邮件 时间:2021-09-04 阅读:(
)
java中如何实现公司邮箱发送邮件配置
Java中可以通过Javamail API实现公司邮箱邮件发送配置,Java mail是利用现有的邮箱账户发送邮件的工具,具体步骤如如下:
1、通过JavamailAPI设置发送者邮箱用户名及密码
2、通过JavamailAPI设置邮件主题、邮件内容、附件及邮件发送时间
3、通过JavamailAPI设置发送者邮箱地址及接收者邮箱地址,接收者地址可以是多个及抄送
4、邮件的需基本元素都设置完毕后,即可通过Javamail API的发送接口执行发送操作。
java编写邮件收发程序步骤
补充回答:如果不涉及到MIME,只是收发简单文本邮件。
就会比较简单。
因为就像你说的,smtp和pop3协议就是文本格式的。
你用Socket建立连接后,就像读文件一样,一行一行的读数据,然后按照协议分析数据格式。
例如如果开始时USER那么后面可能跟的就是用户等。
就是一个字符串解析和匹配的过程。
网上好像有这类代码,自己找一个学习一下。
我以前用C在68k上作过,不是很难。
不过代码已经没有了。
你的是邮件客户端,功能类似于outlook,foxmail等,如果你能上网,就不需要邮件服务器,直接用自己的邮件账号来测试,例如如果你有一个sina的邮件账户,就可以从sina服务器收发邮件。
不过为了测试方便,还是上网去收一个邮件服务器把。
例如foxmail server等。
网上有教程,可以配置好的。
对于编程,既然要求用socket编写,那么就要熟悉java的socket编程,这部分倒是很简单。
可能还需要看看多线程部分。
另外,你需要学习一下, smtp和pop3这两个网络协议的内容,因为你需要解析这两个协议。
如果需要收发代附件的,还需要看mime的内容,不过估计不需要。
其实java有自己的类库javamail,可以实现手法邮件,功能很强大。
不过既然要求必须用socket,那就不能这个了。
说的太罗嗦了。
总结一下:
1)配置邮件服务器,搭建测试环境
2)熟悉java的socket编程、 Swing编程(编写界面)、多线程(选用)
3)熟悉smtp和pop3的内容(内容很多,了解基本的就可以)
网上应该有类似代码,可以用来参考。
java实现发送邮件功能
要实现邮件发送功能需要导入包:mail.jar
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
.demo.action;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import Address;
import .MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
.apache.struts.action.Action;
.apache.struts.action.ActionForm;
.apache.struts.action.ActionForward;
.apache.struts.action.ActionMapping;
.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 = ""; // 发送邮件服务器
String user = "q0000015369"; // 邮件服务器登录用户名
String password = "Queshuwen26"; // 邮件服务器登录密码
String from = "q0000015369@"; //
String to = "c0000016205@"; // 收件人邮件地址
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);
Address fromAddress = new Address(from);
message.setFrom(fromAddress);
Address toAddress = new Address(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("");
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("fail");
}
}
}怎么用JAVA实现邮件发送
一个小例子,也可使用其他api
import?java.util.Properties;
import?javax.mail.Address;
import?javax.mail.Authenticator;
import?javax.mail.BodyPart;
import?javax.mail.Message;
import?javax.mail.Multipart;
import?javax.mail.PasswordAuthentication;
import?javax.mail.Session;
import?javax.mail.Transport;
import?Address;
import?.MimeBodyPart;
import?.MimeMessage;
import?.MimeMultipart;
import?.MimeUtility;
public?class?Test?{
//?test
public?static?void?main(String[]?args)?throws?Exception?{
sendEmail("",?"测试",?"测试",?"!!!收件人地址!!!",?"!!!发件人邮箱用户名!!!",?"!!!邮箱密码!!!",?"发件人昵称");
}
/**
?*?
?*?@param?smtp
?*?@throws?Exception?
?*/
public?static?void?sendEmail(String?emailServer,?String?subject,?String?mailBody,?String?receiver,?final?String?username,?final?String?password,?String?nickname)?throws?Exception?{
Properties?props?=?new?Properties();
props.put("mail.smtp.auth",?"true");
props.setProperty("mail.transport.protocol",?"smtp");
props.setProperty("mail.smtp.host",?emailServer);
props.setProperty("mail.smtp.socketFactory.class",?".ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback",?"false");
props.setProperty("mail.smtp.port",?"465");
props.setProperty("mail.smtp.socketFactory.port",?"465");
Session?session?=?Session.getDefaultInstance(props,?new?Authenticator()?{
protected?PasswordAuthentication?getPasswordAuthentication()?{
return?new?PasswordAuthentication(username,?password);
}
});
session.setDebug(true);
MimeMessage?mimeMsg?=?new?MimeMessage(session);
Multipart?mp?=?new?MimeMultipart();
mimeMsg.setSubject(MimeUtility.encodeText(subject,?"utf-8",?null));
nickname?=?MimeUtility.encodeText(nickname,?"utf-8",?null);
mimeMsg.setFrom(new?Address(username,?nickname,?"UTF-8"));
BodyPart?bp?=?new?MimeBodyPart();
bp.setContent(mailBody,?"text/html;charset=utf-8");
mp.addBodyPart(bp);
mimeMsg.setRecipients(Message.RecipientType.TO,?Address.parse(receiver));
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Transport?transport?=?session.getTransport();
transport.connect(username,?password);
Address[]?allRecipients?=?mimeMsg.getAllRecipients();
transport.sendMessage(mimeMsg,?allRecipients);
transport.close();
}
}java 发送邮件 需要哪些jar包
使用JavaMail发送邮件需要用到mail.jar和activtion.jar两个包。
该类实现了较完整的邮件发送功能,包括以HTML格式发送,添加附件和抄送人。
下面是具体的代码:
.cgw.util.mail;??
import?java.util.Properties;??
import?javax.activation.DataHandler;??
import?javax.activation.FileDataSource;??
import?javax.mail.Address;??
import?javax.mail.BodyPart;??
import?javax.mail.Message;??
import?javax.mail.Multipart;??
import?javax.mail.Session;??
import?javax.mail.Transport;??
import?Address;??
import?.MimeBodyPart;??
import?.MimeMessage;??
import?.MimeMultipart;??
public?class?Mail?{???
????private?MimeMessage?mimeMsg;?//MIME邮件对象???
????private?Session?session;?//邮件会话对象???
????private?Properties?props;?//系统属性???
????private?boolean?needAuth?=?false;?//smtp是否需要认证???
????//smtp认证用户名和密码???
????private?String?username;???
????private?String?password;???
????private?Multipart?mp;?//Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象???
????/**?
?????*?Constructor?
?????*?@param?smtp?邮件发送服务器?
?????*/??
????public?Mail(String?smtp){???
????????setSmtpHost(smtp);???
????????createMimeMessage();???
????}???
????/**?
?????*?设置邮件发送服务器?
?????*?@param?hostName?String??
?????*/??
????public?void?setSmtpHost(String?hostName)?{???
????????System.out.println("设置系统属性:mail.smtp.host?=?"+hostName);???
????????if(props?==?null)??
????????????props?=?System.getProperties();?//获得系统属性对象????
????????props.put("mail.smtp.host",hostName);?//设置SMTP主机???
????}???
????/**?
?????*?创建MIME邮件对象???
?????*?@return?
?????*/??
????public?boolean?createMimeMessage()???
????{???
????????try?{???
????????????System.out.println("准备获取邮件会话对象!");???
????????????session?=?Session.getDefaultInstance(props,null);?//获得邮件会话对象???
????????}???
????????catch(Exception?e){???
????????????System.err.println("获取邮件会话对象时发生错误!"+e);???
????????????return?false;???
????????}???
????????System.out.println("准备创建MIME邮件对象!");???
????????try?{???
????????????mimeMsg?=?new?MimeMessage(session);?//创建MIME邮件对象???
????????????mp?=?new?MimeMultipart();???
????????????return?true;???
????????}?catch(Exception?e){???
????????????System.err.println("创建MIME邮件对象失败!"+e);???
????????????return?false;???
????????}???
????}?????
????/**?
?????*?设置SMTP是否需要验证?
?????*?@param?need?
?????*/??
????public?void?setNeedAuth(boolean?need)?{???
????????System.out.println("设置smtp身份认证:mail.smtp.auth?=?"+need);???
????????if(props?==?null)?props?=?System.getProperties();???
????????if(need){???
????????????props.put("mail.smtp.auth","true");???
????????}else{???
????????????props.put("mail.smtp.auth","false");???
????????}???
????}???
????/**?
?????*?设置用户名和密码?
?????*?@param?name?
?????*?@param?pass?
?????*/??
????public?void?setNamePass(String?name,String?pass)?{???
????????username?=?name;???
????????password?=?pass;???
????}???
????/**?
?????*?设置邮件主题?
?????*?@param?mailSubject?
?????*?@return?
?????*/??
????public?boolean?setSubject(String?mailSubject)?{???
????????System.out.println("设置邮件主题!");???
????????try{???
????????????mimeMsg.setSubject(mailSubject);???
????????????return?true;???
????????}???
????????catch(Exception?e)?{???
????????????System.err.println("设置邮件主题发生错误!");???
????????????return?false;???
????????}???
????}??
????/**??
?????*?设置邮件正文?
?????*?@param?mailBody?String??
?????*/???
????public?boolean?setBody(String?mailBody)?{???
????????try{???
????????????BodyPart?bp?=?new?MimeBodyPart();???
????????????bp.setContent(""+mailBody,"text/html;charset=GBK");???
????????????mp.addBodyPart(bp);???
????????????return?true;???
????????}?catch(Exception?e){???
????????System.err.println("设置邮件正文时发生错误!"+e);???
????????return?false;???
????????}???
????}???
????/**??
?????*?添加附件?
?????*?@param?filename?String??
?????*/???
????public?boolean?addFileAffix(String?filename)?{???
????????System.out.println("增加邮件附件:"+filename);???
????????try{???
????????????BodyPart?bp?=?new?MimeBodyPart();???
????????????FileDataSource?fileds?=?new?FileDataSource(filename);???
????????????bp.setDataHandler(new?DataHandler(fileds));???
????????????bp.setFileName(fileds.getName());???
????????????mp.addBodyPart(bp);???
????????????return?true;???
????????}?catch(Exception?e){???
????????????System.err.println("增加邮件附件:"+filename+"发生错误!"+e);???
????????????return?false;???
????????}???
????}???
????/**??
?????*?设置发信人?
?????*?@param?from?String??
?????*/???
????public?boolean?setFrom(String?from)?{???
????????System.out.println("设置发信人!");???
????????try{???
????????????mimeMsg.setFrom(new?Address(from));?//设置发信人???
????????????return?true;???
????????}?catch(Exception?e)?{???
????????????return?false;???
????????}???
????}???
????/**??
?????*?设置收信人?
?????*?@param?to?String??
?????*/???
????public?boolean?setTo(String?to){???
????????if(to?==?null)return?false;???
????????try{???
????????????mimeMsg.setRecipients(Message.RecipientType.TO,Address.parse(to));???
????????????return?true;???
????????}?catch(Exception?e)?{???
????????????return?false;???
????????}?????
????}???
????/**??
?????*?设置抄送人?
?????*?@param?copyto?String???
?????*/???
????public?boolean?setCopyTo(String?copyto)???
????{???
????????if(copyto?==?null)return?false;???
????????try{???
????????mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])Address.parse(copyto));???
????????return?true;???
????????}???
????????catch(Exception?e)???
????????{?return?false;?}???
????}???
????/**??
?????*?发送邮件?
?????*/???
????public?boolean?sendOut()???
????{???
????????try{???
????????????mimeMsg.setContent(mp);???
????????????mimeMsg.saveChanges();???
????????????System.out.println("正在发送邮件....");???
????????????Session?mailSession?=?Session.getInstance(props,null);???
????????????Transport?transport?=?mailSession.getTransport("smtp");???
????????????transport.connect((String)props.get("mail.smtp.host"),username,password);???
????????????transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));???
????????????transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC));???
????????????//transport.send(mimeMsg);???
????????????System.out.println("发送邮件成功!");???
????????????transport.close();???
????????????return?true;???
????????}?catch(Exception?e)?{???
????????????System.err.println("邮件发送失败!"+e);???
????????????return?false;???
????????}???
????}???
????/**?
?????*?调用sendOut方法完成邮件发送?
?????*?@param?smtp?
?????*?@param?from?
?????*?@param?to?
?????*?@param?subject?
?????*?@param?content?
?????*?@param?username?
?????*?@param?password?
?????*?@return?boolean?
?????*/??
????public?static?boolean?send(String?smtp,String?from,String?to,String?subject,String?content,String?username,String?password)?{??
????????Mail?theMail?=?new?Mail(smtp);??
????????theMail.setNeedAuth(true);?//需要验证??
????????if(!theMail.setSubject(subject))?return?false;??
????????if(!theMail.setBody(content))?return?false;??
????????if(!theMail.setTo(to))?return?false;??
????????if(!theMail.setFrom(from))?return?false;??
????????theMail.setNamePass(username,password);??
????????if(!theMail.sendOut())?return?false;??
????????return?true;??
????}??
????/**?
?????*?调用sendOut方法完成邮件发送,带抄送?
?????*?@param?smtp?
?????*?@param?from?
?????*?@param?to?
?????*?@param?copyto?
?????*?@param?subject?
?????*?@param?content?
?????*?@param?username?
?????*?@param?password?
?????*?@return?boolean?
?????*/??
????public?static?boolean?sendAndCc(String?smtp,String?from,String?to,String?copyto,String?subject,String?content,String?username,String?password)?{??
????????Mail?theMail?=?new?Mail(smtp);??
????????theMail.setNeedAuth(true);?//需要验证??
????????if(!theMail.setSubject(subject))?return?false;??
????????if(!theMail.setBody(content))?return?false;??
????????if(!theMail.setTo(to))?return?false;??
????????if(!theMail.setCopyTo(copyto))?return?false;??
????????if(!theMail.setFrom(from))?return?false;??
????????theMail.setNamePass(username,password);??
????????if(!theMail.sendOut())?return?false;??
????????return?true;??
????}??
????/**?
?????*?调用sendOut方法完成邮件发送,带附件?
?????*?@param?smtp?
?????*?@param?from?
?????*?@param?to?
?????*?@param?subject?
?????*?@param?content?
?????*?@param?username?
?????*?@param?password?
?????*?@param?filename?附件路径?
?????*?@return?
?????*/??
????public?static?boolean?send(String?smtp,String?from,String?to,String?subject,String?content,String?username,String?password,String?filename)?{??
????????Mail?theMail?=?new?Mail(smtp);??
????????theMail.setNeedAuth(true);?//需要验证??
????????if(!theMail.setSubject(subject))?return?false;??
????????if(!theMail.setBody(content))?return?false;??
????????if(!theMail.addFileAffix(filename))?return?false;???
????????if(!theMail.setTo(to))?return?false;??
????????if(!theMail.setFrom(from))?return?false;??
????????theMail.setNamePass(username,password);??
????????if(!theMail.sendOut())?return?false;??
????????return?true;??
????}??
????/**?
?????*?调用sendOut方法完成邮件发送,带附件和抄送?
?????*?@param?smtp?
?????*?@param?from?
?????*?@param?to?
?????*?@param?copyto?
?????*?@param?subject?
?????*?@param?content?
?????*?@param?username?
?????*?@param?password?
?????*?@param?filename?
?????*?@return?
?????*/??
????public?static?boolean?sendAndCc(String?smtp,String?from,String?to,String?copyto,String?subject,String?content,String?username,String?password,String?filename)?{??
????????Mail?theMail?=?new?Mail(smtp);??
????????theMail.setNeedAuth(true);?//需要验证??
????????if(!theMail.setSubject(subject))?return?false;??
????????if(!theMail.setBody(content))?return?false;??
????????if(!theMail.addFileAffix(filename))?return?false;???
????????if(!theMail.setTo(to))?return?false;??
????????if(!theMail.setCopyTo(copyto))?return?false;??
????????if(!theMail.setFrom(from))?return?false;??
????????theMail.setNamePass(username,password);??
????????if(!theMail.sendOut())?return?false;??
????????return?true;??
????}??
}java 发送邮件
要两个java文件 还有一个mail.jar是不是只能用javamail谁也不敢说
第一个:
public class Constant {
public static final String mailAddress ="用户名@";
public static final String mailCount ="用户名";
public static final String mailPassword ="密码";
public static final String mailServer ="";
//pukeyouxintest,
}
第二个:
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import Address;
import .MimeMessage;
public class SendMail {
/**
* 发送简单邮件
* @param str_from:发件人地址
* @param str_to:收件人地址
* @param str_title:邮件标题
* @param str_content:邮件正文
*/
public static void send(String str_from,String str_to,String str_title,String str_content) {
// str_content="<a href='>html元素</a>"; //for testing send html mail!
try {
//建立邮件会话
Properties props=new Properties(); //用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,
//存储发送邮件服务器的信息
props.put("mail.smtp.host",Constant.mailServer);
//同时通过验证
props.put("mail.smtp.auth","true");
//根据属性新建一个邮件会话
Session s=Session.getInstance(props);
s.setDebug(true); //有他会打印一些调试信息。
//由邮件会话新建一个消息对象
MimeMessage message=new MimeMessage(s);
//设置邮件
Address from= new Address(str_from); //pukeyouxintest2@
message.setFrom(from); //设置发件人的地址
//
// //设置收件人,并设置其接收类型为TO
Address to=new Address(str_to); //pukeyouxintest3@
message.setRecipient(Message.RecipientType.TO, to);
//设置标题
message.setSubject(str_title); //java学习
//设置信件内容
// message.setText(str_content); //发送文本邮件 //你好吗?
message.setContent(str_content, "text/html;charset=gbk"); //发送HTML邮件 //<b>你好</b><br><p>大家好</p>
//设置发信时间
message.setSentDate(new Date());
//存储邮件信息
message.saveChanges();
//发送邮件
Transport transport=s.getTransport("smtp");
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect(Constant.mailServer,Constant.mailCount,Constant.mailPassword);
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message,message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//测试用的,你吧你想写的内容写上去就行
send(Constant.mailAddress,"收件人邮箱","标题","<b>内容</b>");
}
}
然后把mail.jar导入,就可以了,我用的是163 的,其他的吧相应的服务器改一下就行了
华纳云(HNCloud Limited)是一家专业的全球数据中心基础服务提供商,总部在香港,隶属于香港联合通讯国际有限公司,拥有香港政府颁发的商业登记证明,保证用户的安全性和合规性。 华纳云是APNIC 和 ARIN 会员单位。主要提供数据中心基础服务、互联网业务解决方案, 以及香港服务器租用、香港服务器托管、香港云服务器、美国云服务器,云计算、云安全技术研发等产品和服务。其中云服务器基于成熟的 ...
6元虚拟主机是否值得购买?近期各商家都纷纷推出了优质便宜的虚拟主机产品,其中不少6元的虚拟主机,这种主机是否值得购买,下面我们一起来看看。1、百度云6元体验三个月(活动时间有限抓紧体验)体验地址:https://cloud.baidu.com/campaign/experience/index.html?from=bchPromotion20182、Ucloud 10元云主机体验地址:https:...
puaex怎么样?puaex是一家去年成立的国人商家,本站也分享过几次,他家主要销售香港商宽的套餐,给的全部为G口带宽,而且是不限流量的,目前有WTT和HKBN两种线路的方面,虽然商家的价格比较贵,但是每次补一些货,就会被抢空,之前一直都是断货的状态,目前商家进行了补货,有需要这种类型机器的朋友可以入手。点击进入:puaex商家官方网站Puaex香港vds套餐:全部为KVM虚拟架构,G口的带宽,可...
java发邮件为你推荐
php文件php 格式文件 怎么打开?急!!!fast路由器路由器fast怎么设置无线网络路由器映射路由器映射是什么意思活跃网络移动大V网是什么意思?摇一摇周边微信摇一摇周边红包设置soap是什么意思肥皂剧是什么意思?电商网站设计电商网站设计需要注意哪些修复网络lspLSP修复是什么意思?gps简介GPS有哪些功能趋势防毒如何破解趋势防病毒墙
安徽虚拟主机 播放vps上的视频 企业域名备案 80vps uk2 ix主机 天猫双十一秒杀 免费个人博客 免费静态空间 台湾谷歌网址 毫秒英文 seednet ntfs格式分区 息壤代理 无限流量 架设邮件服务器 秒杀品 97rb googlevoice forwarder 更多