「javaapi邮件」Java邮件
今天给各位分享javaapi邮件的知识,其中也会对Java邮件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中如何实现公司邮箱发送邮件配置
- 2、通过ews java api怎么自动获取exchange的邮件
- 3、Java收发邮件过程中具体的功能是怎么实现的
- 4、使用Java MAil实现电子邮件客户端的设计有什么特点?
- 5、java编写小型的局域网邮件发送
java中如何实现公司邮箱发送邮件配置
Java中可以通过Javamail API实现公司邮箱邮件发送配置,Java mail是利用现有的邮箱账户发送邮件的工具,具体步骤如如下:
1、通过JavamailAPI设置发送者邮箱用户名及密码
2、通过JavamailAPI设置邮件主题、邮件内容、附件及邮件发送时间
3、通过JavamailAPI设置发送者邮箱地址及接收者邮箱地址,接收者地址可以是多个及抄送
4、邮件的需基本元素都设置完毕后,即可通过Javamail API的发送接口执行发送操作。
通过ews java api怎么自动获取exchange的邮件
一、通过Exchange Web Service来读取
1、首先,在项目上添加Web Service引用,这个Web Service的URL 地址格式如:https:// Exchange邮件系统的服务器名/EWS/Exchange.asmx
2、引入如下命名空间:
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
3、编写代码读取邮件信息:
//忽略SSL证书请求必须的,不添加在执行时会报错,错误信息好像是(记不清了)“客户端响应错误………html / text”
ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ return true; };
//创建Exchange服务绑定对象
ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
//创建安全身份凭证
ICredentials creds = new NetworkCredential("username", "password", "domain");
//建立信任连接
exchangeServer.Credentials = creds;
exchangeServer.Url = "https:// Exchange邮件系统的服务器名/EWS/Exchange.asmx";
Java收发邮件过程中具体的功能是怎么实现的
1.SMTP协议
用户连上邮件服务器后,要想给它发送一封电子邮件,需要遵循一定的通迅规则,SMTP协议就是用于定义这种通讯规则的。
因而,通常我们也把处理用户smtp请求(邮件发送请求)的邮件服务器称之为SMTP服务器。(25)
2.POP3协议
同样,用户若想从邮件服务器管理的电子邮箱中接收一封电子邮件的话,他连上邮件服务器后,也需要遵循一定的通迅格式,POP3协议用于定义这种通讯格式。
因而,通常我们也把处理用户pop3请求(邮件接收请求)的邮件服务器称之为POP3服务器。(110)
下图用于演示两帐户相互发送邮件的过程
3.1JavaMail API按其功能划分通常可分为如下三大类:
创建和解析邮件内容的API :Message类是创建和解析邮件的核心API,它的实例对象代表一封电子邮件。
3.2发送邮件的API:Transport类是发送邮件的核心API类,它的实例对象代表实现了某个邮件发送协议的邮件发送对象,例如SMTP协议。
接收邮件的API:Store类是接收邮件的核心API类,它的实例对象代表实现了某个邮件接收协议的邮件接收对象,例如POP3协议。
3.3Session类
Session类用于定义整个应用程序所需的环境信息,以及收集客户端与邮件服务器建立网络连接的会话信息,如邮件服务器的主机名、端口号、采用的邮件发送和接收协议等。Session对象根据这些信息构建用于邮件收发的Transport和Store对象,以及为客户端创建Message对象时提供信息支持。
4.邮件组织结构相关的API
MimeMessage类表示整封邮件。
MimeBodyPart类表示邮件的一个MIME消息。
MimeMultipart类表示一个由多个MIME消息组合成的组合MIME消息。
5.具体的例子程序
package cn.edu.dlmu.send;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class SendMail {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
//连接的邮件服务器的主机名
prop.setProperty("mail.smtp.host", "smtp.sina.com.cn");
//发送邮件的协议
prop.setProperty("mail.transport.protocol", "smtp");
//是否向邮件服务器提交认证
prop.setProperty("mail.smtp.auth", "true");
//创建session
Session session = Session.getInstance(prop);
session.setDebug(true);
//得到transport
Transport ts = session.getTransport();
//连接邮件服务器
ts.connect("smtp.sina.com.cn", "xxxx@sina.com", "xxxxx");
//发送邮件
MimeMessage message = createMessage(session);
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
public static MimeMessage createMessage(Session session) throws Exception {
MimeMessage message = new MimeMessage(session);
//设置邮件的基本信息
message.setFrom(new InternetAddress("xxxx@sina.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("1219070362@qq.com"));
message.setSubject("test");
//正文
MimeBodyPart text = new MimeBodyPart();
//设置charaset可以解决中文正文的乱码问题,内嵌可下载的图片
text.setContent("你好xxx,img src='c:/dog.jpg' /测试成功!br/img src='cid:aaa.jpg' /", "text/html;charset=gbk");
//图片1
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src/88.jpg")));
image.setContentID("aaa.jpg");
//附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/javamail架包.jar"));
attach.setDataHandler(dh);
//解决文件中文乱码问题
attach.setFileName(MimeUtility.encodeText(dh.getName()));
//描述正文和图片的关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(image);
mp.setSubType("related");
//描述正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
//将正文封装为一个body
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();
return message;
}
}
使用Java MAil实现电子邮件客户端的设计有什么特点?
没什么优点,JAVA邮件API只是为了在企业级应用架构中构建模块的一部分来完善一套系统的基础功能。
如果用JAVA来开发邮件客户端程序的话确实有些勉强,其远远比不上其他语言开发的客户端程序的。
其最大的好处也就是让程序员迅速熟悉JAVA MAIL 的API吧。其他的我还真的想不出来它会相比其他的会有什么优势。
java编写小型的局域网邮件发送
我给你提供一个我在项目里面实际使用的代码.
这是我基于一个网上的代码自己修改封装过来的.
你可以参考一下
/**
*
* @author Sglee
*
*/
public class SimpleMail {
private static String encode = null;
static {
if ("\\".equals(File.separator)) {
encode = "GBK";
} else {
encode = "UTF-8";
}
}
/**
* 以文本格式发送邮件
*
* @param mailInfo
* @return
*/
public static boolean sendTextMail(MailInfo mailInfo) {
for (int i = 0; i 3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// mailMessage.setText(mailInfo.getContent());//设置邮件消息的主要内容
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含附件内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != null filePaths.length 0) {
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ file.getName());// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
/**
* 将string[]包装成EmailAddress
* @param mailInfo
* @return
* @throws AddressException
*/
private static Address [] wrapAddress(String[] adds) throws AddressException {
// String[] adds = mailInfo.getToAddress();
if(adds == null || adds.length == 0){
return null;
}
Address []to = new Address[adds.length];
for(int i = 0;iadds.length;i++){
to[i]=new InternetAddress(adds[i]);
}
return to;
}
/**
* 以HTML格式发送邮件
*
* @param mailInfo
* @return
*/
public static boolean sendHtmlMail(MailInfo mailInfo) {
for (int i = 0; i 3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含HTML内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != null filePaths.length 0) {
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ enc.encode(EmailFileNameConvert.changeFileName(file.getName()).getBytes())
+ "?=");// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
}
/**
* 封装邮件的基本信息
*
* @author Sglee
*
*/
public class MailInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3937199642590071261L;
private String mailServerHost;// 服务器ip
private String mailServerPort;// 端口
private long timeout;// 超时时间
private String fromAddress;// 发送者的邮件地址
private String[] toAddress;// 邮件接收者地址
private String[] msAddress;// 密送地址
private String username;// 登录邮件发送服务器的用户名
private String password;// 登录邮件发送服务器的密码
private boolean validate = false;// 是否需要身份验证
private String subject;// 邮件主题
private String content;// 邮件内容
private String[] attachFileNames;// 附件的文件地址
private boolean debug;// 调试模式
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
p.put("mail.smtp.timeout", this.timeout);
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String[] getToAddress() {
return toAddress;
}
public void setToAddress(String[] toAddress) {
this.toAddress = toAddress;
}
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
public void setMsAddress(String[] msAddress) {
this.msAddress = msAddress;
}
public String[] getMsAddress() {
return msAddress;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public boolean isDebug() {
return debug;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public long getTimeout() {
return timeout;
}
}
public class MyAuthenticator extends Authenticator {
private String username = null;
private String password = null;
public MyAuthenticator() {
};
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
注意一下:
Myeclipse自带的JavaEE5.jar和java mail会发生冲突
找到ME下的javeee包
D:\MyEclipse 8.5\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5\javaee.jar
用rar等解压工具解开javaee.jar,删除里面的javax\mail文件夹(可以先备份javaee.jar)
也即,以后都不能使用javaee.jar里面的邮件api发送邮件了.
javaapi邮件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java邮件、javaapi邮件的信息别忘了在本站进行查找喔。