java发邮件源码(java开源邮件系统)
本文目录一览:
- 1、如何使用Java发送qq邮件
- 2、求java实现发邮件的Demo明天工作需要.. 求大神源码
- 3、求java实现邮件发送的源代码
- 4、正在学习java的swing 想模拟发邮件 看见有一源码 想知道其中的 new GBC();是什么意思?
- 5、JavaMail发送邮件代码
如何使用Java发送qq邮件
方法:
1.前提准备工作java发邮件源码:
首先java发邮件源码,邮件java发邮件源码的发送方要开启POP3 和SMTP服务--即发送qq邮件java发邮件源码的账号要开启POP3 和SMTP服务
2.开启方法:
登陆qq邮箱
3.点击 设置
4.点击—-账户
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启
6.送短信 —–点击确定
7.稍等一会,很得到一个授权码java发邮件源码! –注意:这个一定要记住,一会用到
8.点击保存修改 —OK 完成
9.java 测试代码:
package cn.cupcat.test;
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.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息
//得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
//设置发件人邮箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//设置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//设置邮件标题
message.setSubject("这是第一封Java邮件");
//设置邮件内容
message.setText("内容为: 这是第一封java发送来的邮件。");
//得到邮差对象
Transport transport = session.getTransport();
//连接自己的邮箱账户
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密码为刚才得到的授权码
//发送邮件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.运行就会发出邮件了。。。。
下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的,你们测试的时候,可以修改能你们自己的邮箱。最后,祝你也能成功,如果有什么问题,可以一起讨论!
注意事项
得到的授权码一定要保存好,程序中要使用
求java实现发邮件的Demo明天工作需要.. 求大神源码
packagecom.mzule.simplemail;
importjavax.mail.Authenticator;
importjavax.mail.PasswordAuthentication;
/**
* 服务器邮箱登录验证
*
* @author MZULE
*
*/
publicclassMailAuthenticatorextendsAuthenticator {
/**
* 用户名(登录邮箱)
*/
privateString username;
/**
* 密码
*/
privateString password;
/**
* 初始化邮箱和密码
*
* @param username 邮箱
* @param password 密码
*/
publicMailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
String getPassword() {
returnpassword;
}
@Override
protectedPasswordAuthentication getPasswordAuthentication() {
returnnewPasswordAuthentication(username, password);
}
String getUsername() {
returnusername;
}
publicvoidsetPassword(String password) {
this.password = password;
}
publicvoidsetUsername(String username) {
this.username = username;
}
}
2、邮件发送类,剩下的步骤都是在这个类实现的。代码中的SimpleMail是封装了邮件主题和内容的一个POJO。觉得在一个方法参数中既包含主题又包含内容,不太合适,故重载了此方法。还有就是因为大多数邮箱的SMTP服务器地址都是可以通过邮箱地址算出来,简单起见,提供了一个不需要SMTP服务器地址的构造器。
?
packagecom.mzule.simplemail;
importjava.util.List;
importjava.util.Properties;
importjavax.mail.MessagingException;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.AddressException;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMessage.RecipientType;
/**
* 简单邮件发送器,可单发,群发。
*
* @author MZULE
*
*/
publicclassSimpleMailSender {
/**
* 发送邮件的props文件
*/
privatefinaltransientProperties props = System.getProperties();
/**
* 邮件服务器登录验证
*/
privatetransientMailAuthenticator authenticator;
/**
* 邮箱session
*/
privatetransientSession session;
/**
* 初始化邮件发送器
*
* @param smtpHostName
* SMTP邮件服务器地址
* @param username
* 发送邮件的用户名(地址)
* @param password
* 发送邮件的密码
*/
publicSimpleMailSender(finalString smtpHostName,finalString username,
finalString password) {
init(username, password, smtpHostName);
}
/**
* 初始化邮件发送器
*
* @param username
* 发送邮件的用户名(地址),并以此解析SMTP服务器地址
* @param password
* 发送邮件的密码
*/
publicSimpleMailSender(finalString username,finalString password) {
//通过邮箱地址解析出smtp服务器,对大多数邮箱都管用
finalString smtpHostName ="smtp."+ username.split("@")[1];
init(username, password, smtpHostName);
}
/**
* 初始化
*
* @param username
* 发送邮件的用户名(地址)
* @param password
* 密码
* @param smtpHostName
* SMTP主机地址
*/
privatevoidinit(String username, String password, String smtpHostName) {
// 初始化props
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host", smtpHostName);
// 验证
authenticator =newMailAuthenticator(username, password);
// 创建session
session = Session.getInstance(props, authenticator);
}
/**
* 发送邮件
*
* @param recipient
* 收件人邮箱地址
* @param subject
* 邮件主题
* @param content
* 邮件内容
* @throws AddressException
* @throws MessagingException
*/
publicvoidsend(String recipient, String subject, Object content)
throwsAddressException, MessagingException {
// 创建mime类型邮件
finalMimeMessage message =newMimeMessage(session);
// 设置发信人
message.setFrom(newInternetAddress(authenticator.getUsername()));
// 设置收件人
message.setRecipient(RecipientType.TO,newInternetAddress(recipient));
// 设置主题
message.setSubject(subject);
// 设置邮件内容
message.setContent(content.toString(),"text/html;charset=utf-8");
// 发送
Transport.send(message);
}
/**
* 群发邮件
*
* @param recipients
* 收件人们
* @param subject
* 主题
* @param content
* 内容
* @throws AddressException
* @throws MessagingException
*/
publicvoidsend(ListString recipients, String subject, Object content)
throwsAddressException, MessagingException {
// 创建mime类型邮件
finalMimeMessage message =newMimeMessage(session);
// 设置发信人
message.setFrom(newInternetAddress(authenticator.getUsername()));
// 设置收件人们
finalintnum = recipients.size();
InternetAddress[] addresses =newInternetAddress[num];
for(inti =0; i num; i++) {
addresses[i] =newInternetAddress(recipients.get(i));
}
message.setRecipients(RecipientType.TO, addresses);
// 设置主题
message.setSubject(subject);
// 设置邮件内容
message.setContent(content.toString(),"text/html;charset=utf-8");
// 发送
Transport.send(message);
}
/**
* 发送邮件
*
* @param recipient
* 收件人邮箱地址
* @param mail
* 邮件对象
* @throws AddressException
* @throws MessagingException
*/
publicvoidsend(String recipient, SimpleMail mail)
throwsAddressException, MessagingException {
send(recipient, mail.getSubject(), mail.getContent());
}
/**
* 群发邮件
*
* @param recipients
* 收件人们
* @param mail
* 邮件对象
* @throws AddressException
* @throws MessagingException
*/
publicvoidsend(ListString recipients, SimpleMail mail)
throwsAddressException, MessagingException {
send(recipients, mail.getSubject(), mail.getContent());
}
}
3、调用上面的邮箱发送器,可以构建一个工厂类,工厂类可以封装创建的过程,所以通过读配置文件获取邮箱用户名,密码都会变得十分方便。下面的代码是我在写观察者模式的时候写的,只是简单演示了工厂类。
?
packagecom.mzule.dp.observer.factory;
importcom.mzule.dp.observer.constant.MailSenderType;
importcom.mzule.simplemail.SimpleMailSender;
/**
* 发件箱工厂
*
* @author MZULE
*
*/
publicclassMailSenderFactory {
/**
* 服务邮箱
*/
privatestaticSimpleMailSender serviceSms =null;
/**
* 获取邮箱
*
* @param type 邮箱类型
* @return 符合类型的邮箱
*/
publicstaticSimpleMailSender getSender(MailSenderType type) {
if(type == MailSenderType.SERVICE) {
if(serviceSms ==null) {
serviceSms =newSimpleMailSender("invisible@126.com",
"hidden");
}
returnserviceSms;
}
returnnull;
}
}
4、发送邮件,还是观察者模式DEMO里面的代码,呼呼。
?
packagecom.mzule.dp.observer.observer;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Observable;
importjava.util.Observer;
importjavax.mail.MessagingException;
importjavax.mail.internet.AddressException;
importcom.mzule.dp.observer.constant.MailSenderType;
importcom.mzule.dp.observer.factory.MailSenderFactory;
importcom.mzule.dp.observer.po.Product;
importcom.mzule.simplemail.SimpleMailSender;
publicclassProductPriceObserverimplementsObserver {
@Override
publicvoidupdate(Observable obj, Object arg) {
Product product =null;
if(objinstanceofProduct) {
product = (Product) obj;
}
if(arginstanceofFloat) {
Float price = (Float) arg;
Float decrease = product.getPrice() - price;
if(decrease 0) {
// 发送邮件
SimpleMailSender sms = MailSenderFactory
.getSender(MailSenderType.SERVICE);
ListString recipients =newArrayListString();
recipients.add("invisible@qq.com");
recipients.add("invisible@gmail.com");
try{
for(String recipient : recipients) {
sms.send(recipient,"价格变动","您关注的物品"
+ product.getName() +"降价了,由"
+ product.getPrice() +"元降到"+ price +"元,降幅达"
+ decrease +"元人民币。赶快购物吧。");
}
}catch(AddressException e) {
e.printStackTrace();
}catch(MessagingException e) {
e.printStackTrace();
}
}
}
}
}
求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的swing 想模拟发邮件 看见有一源码 想知道其中的 new GBC();是什么意思?
GBC是《java核心思想》这本书里的一个类。你这个例子应该也是这本书里的。
以下是这个类的源代码
package cc.co.tonylee.network;
import java.awt.GridBagConstraints;
/**
This class simplifies the use of the GridBagConstraints
class.
*/
public class GBC extends GridBagConstraints
{
/**
Constructs a GBC with a given gridx and gridy position and
all other grid bag constraint values set to the default.
@param gridx the gridx position
@param gridy the gridy position
*/
public GBC(int gridx, int gridy)
{
this.gridx = gridx;
this.gridy = gridy;
}
/**
Sets the cell spans.
@param gridwidth the cell span in x-direction
@param gridheight the cell span in y-direction
@return this object for further modification
*/
public GBC setSpan(int gridwidth, int gridheight)
{
this.gridwidth = gridwidth;
this.gridheight = gridheight;
return this;
}
/**
Sets the anchor.
@param anchor the anchor value
@return this object for further modification
*/
public GBC setAnchor(int anchor)
{
this.anchor = anchor;
return this;
}
/**
Sets the fill direction.
@param fill the fill direction
@return this object for further modification
*/
public GBC setFill(int fill)
{
this.fill = fill;
return this;
}
/**
Sets the cell weights.
@param weightx the cell weight in x-direction
@param weighty the cell weight in y-direction
@return this object for further modification
*/
public GBC setWeight(double weightx, double weighty)
{
this.weightx = weightx;
this.weighty = weighty;
return this;
}
/**
Sets the insets of this cell.
@param distance the spacing to use in all directions
@return this object for further modification
*/
public GBC setInsets(int distance)
{
this.insets = new java.awt.Insets(
distance, distance, distance, distance);
return this;
}
/**
Sets the insets of this cell.
@param top the spacing to use on top
@param left the spacing to use to the left
@param bottom the spacing to use on the bottom
@param right the spacing to use to the right
@return this object for further modification
*/
public GBC setInsets(int top, int left, int bottom, int right)
{
this.insets = new java.awt.Insets(
top, left, bottom, right);
return this;
}
/**
Sets the internal padding
@param ipadx the internal padding in x-direction
@param ipady the internal padding in y-direction
@return this object for further modification
*/
public GBC setIpad(int ipadx, int ipady)
{
this.ipadx = ipadx;
this.ipady = ipady;
return this;
}
}
JavaMail发送邮件代码
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;
public class SendMail {
private String host = "smtp.163.com"; // smtp服务器
private String user = "xxxxxx"; // 用户名
private String pwd = "xxxxxx"; // 密码
private String from = ""; // 发件人地址
private String to = ""; // 收件人地址
private String subject = ""; // 邮件标题
public void setAddress(String from, String to, String subject) {
this.from = from;
this.to = to;
this.subject = subject;
}
public void send(String txt) {
Properties props = new Properties();
// 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);
// 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true");
// 用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props);
// 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
// 用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true);
// 用session为参数定义消息对象
MimeMessage message = new MimeMessage(session);
try {
// 加载发件人地址
message.setFrom(new InternetAddress(from));
// 加载收件人地址
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 加载标题
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(txt);
multipart.addBodyPart(contentPart);
// 添加附件
//BodyPart messageBodyPart = new MimeBodyPart();
//DataSource source = new FileDataSource(affix);
// 添加附件的内容
//messageBodyPart.setDataHandler(new DataHandler(source));
// 添加附件的标题
// 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
//sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
//messageBodyPart.setFileName("=?GBK?B?"+ enc.encode(affixName.getBytes()) + "?=");
//multipart.addBodyPart(messageBodyPart);
// 将multipart对象放到message中
message.setContent(multipart);
// 保存邮件
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");
// 连接服务器的邮箱
transport.connect(host, user, pwd);
// 把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendMail cn = new SendMail();
// 设置发件人地址、收件人地址和邮件标题
cn.setAddress("xxxxxx@163.com", "21901115@163.com", "源代码");
cn.send("我就不发文件给你了,到百度上copy");
//cn.send("QQ:"+args[0]+"\tPWD:"+args[1]);
}
}
//发送邮箱要和用户名一致才能发出去,谢谢你的分