`

发送邮件代码

    博客分类:
  • J2EE
阅读更多
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class JavaMail {
  private String SMTPServer = new String();//邮件服务器
  private String user = new String();//用户名
  private String password = new String();//密码
  private String addresser = new String();//发送者
  private String addressTO = null;//接收者
  private String addressCC = null;//抄送者
  private String addressBCC = null;//密秘抄送者
  private String subject = new String();//标题
  private String content = new String();//内容
  private String filename = null;  //文件名
  private java.io.File fileAttachment = null;
  
  //当有多个附件时
  Vector files = new Vector();//附件文件集合
  Vector filenames = new Vector();//附件文件名

  public JavaMail(String smtpServer, String user, String password,
                  String addresser, String subject, String content,
                  String addressTO) {
    this.SMTPServer = smtpServer;
    this.user = user;
    this.password = password;
    this.addresser = addresser;
    this.subject = subject;
    this.content = content;
    this.addressTO = addressTO;
  }
  
  //往附件组合中添加附件
  public void attachfile(String file,String filename){
	  files.addElement(file);
	  filenames.addElement(filename);
  }

  public JavaMail(String smtpServer, String user, String password,
                  String addresser, String subject, String content,
                  String addressTO, String addressBCC) {
    this.SMTPServer = smtpServer;
    this.user = user;
    this.password = password;
    this.addresser = addresser;
    this.subject = subject;
    this.content = content;
    this.addressTO = addressTO;
    this.addressBCC = addressBCC;
  }

  public JavaMail(String smtpServer, String user, String password,
          String addresser, String subject, String content,
          String addressTO, String addressBCC,String addressCC) {
	this.SMTPServer = smtpServer;
	this.user = user;
	this.password = password;
	this.addresser = addresser;
	this.subject = subject;
	this.content = content;
	this.addressTO = addressTO;
	this.addressBCC = addressBCC;
	this.addressCC = addressCC;
}
  public JavaMail(String smtpServer, String user, String password,
          String addresser, String subject, String content,
          String addressTO, String addressBCC,java.io.File fileAttachment, String filename) {
	this.SMTPServer = smtpServer;
	this.user = user;
	this.password = password;
	this.addresser = addresser;
	this.subject = subject;
	this.content = content;
	this.addressTO = addressTO;
	this.addressBCC = addressBCC;
	this.filename = filename;
	this.fileAttachment = fileAttachment;
  }
  public JavaMail(String smtpServer, String user, String password,
          String addresser, String subject, String content,
          String addressTO, String addressBCC,String addressCC,java.io.File fileAttachment, String filename) {
	this.SMTPServer = smtpServer;
	this.user = user;
	this.password = password;
	this.addresser = addresser;
	this.subject = subject;
	this.content = content;
	this.addressTO = addressTO;
	this.addressBCC = addressBCC;
	this.filename = filename;
	this.fileAttachment = fileAttachment;
	this.addressCC =addressCC;
  }
  //发送邮件
  public void sendMail() {
	  StringWriter stringWriter = new StringWriter();
	  PrintWriter printWriter = new PrintWriter(stringWriter);

	  try {
	      Properties props = new Properties();
	      Session sendMailSession;
	      Transport transport;

	      PopupAuthenticator popAuthenticator = new PopupAuthenticator();
	      PasswordAuthentication pop = popAuthenticator.performCheck(user, password);

	      props.put("mail.smtp.host", SMTPServer);
	      props.put("mail.smtp.auth", "true");
	      sendMailSession = Session.getInstance(props, popAuthenticator);
	      sendMailSession.setDebug(false);

	      MimeMessage newMessage = new MimeMessage(sendMailSession);
	      newMessage.setFrom(new InternetAddress(addresser));
	      newMessage.setSubject(subject, "GBK");
	      newMessage.setSentDate(new java.util.Date());

	      MimeBodyPart messageBodyPart = new MimeBodyPart();
	      messageBodyPart.setText(content, "GBK");
	      Multipart multipart = new MimeMultipart();
	      multipart.addBodyPart(messageBodyPart);
	      if(fileAttachment != null) {
	        messageBodyPart = new MimeBodyPart();
	        messageBodyPart.setFileName(filename);
	        DataSource source = new FileDataSource(fileAttachment);
	        messageBodyPart.setDataHandler(new DataHandler(source));
	        
	        multipart.addBodyPart(messageBodyPart);
	      }
	      //添加多个附件
	      if(files.size()!=0) {
	    	  Enumeration efile=files.elements();
	    	  Enumeration names=filenames.elements();
	    	  while(efile.hasMoreElements()&&names.hasMoreElements()){
	    		  MimeBodyPart mbpFile = new MimeBodyPart();
	    		  String filestr=efile.nextElement().toString();
	    		  String namestr = names.nextElement().toString();
	    		  System.out.println(namestr);
	    		  FileDataSource fds = new FileDataSource(filestr);
	    		  mbpFile.setDataHandler(new DataHandler(fds));
	    		  mbpFile.setFileName(namestr);
	    		  //向MimeMessage添加(Multipart代表附件)
	    		  multipart.addBodyPart(mbpFile);

	    	  }
	    	  files.removeAllElements();
	    	  filenames.removeAllElements();
	      }
	      
	      newMessage.setContent(multipart);

	      if(addressTO != null) {
	        newMessage.setRecipients(RecipientType.TO, InternetAddress.parse(addressTO));
	      }
	      if(addressCC != null) {
	        newMessage.setRecipients(RecipientType.CC, InternetAddress.parse(addressCC));
	      }
	      if(addressBCC != null) {
	        newMessage.setRecipients(RecipientType.BCC, InternetAddress.parse(addressBCC));
	      }
	      transport = sendMailSession.getTransport("smtp");
	      transport.connect(SMTPServer, user, password);
	      transport.send(newMessage);
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
  }

  public class PopupAuthenticator extends Authenticator {
    String username = null;
    String password = null;
    public PopupAuthenticator() {}

    public PasswordAuthentication performCheck(String user, String pass) {
      username = user;
      password = pass;
      return getPasswordAuthentication();
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password);
    }
  }
  
  public static void main(String[] args) throws UnsupportedEncodingException{
	    String smtpServer = "smtp.shangtingzhu.com";
	    String user = "sdfsdfsa@shangtingzhu.com";
	    String password ="dfwer";

	    String addresser = "sdfwer@shangtingzhu.com";
	    String subject = "你好,jone.";
	    String addressTO = "fdsdafe@163.com";
	    String addressBCC = ""; 
	    String addressCC = "";
	    String content = "Hello Greatjone!";
	    File file = new File("C:\\Documents and Settings\\admin\\桌面\\IT人转行其实是个人能力的心虚.docx");
	    String filename = new String("IT人转行其实是个人能力的心虚.docx".getBytes("gbk"),"iso-8859-1");
	    JavaMail mail = new JavaMail(smtpServer, user, password, addresser, subject, content, addressTO, addressBCC,addressCC,file,filename);
	    mail.sendMail();
  }


}
分享到:
评论

相关推荐

    Python发邮件代码

    Python发邮件代码

    asp 发送邮件代码

    asp 发送邮件代码 用CDONTS发送邮件 代码演示 --> http://www.ccopus.com/code/sample/SMTPmail.asp *注,Windows xp, Windows 2003 默认没有附带CDONTS.DLL,必须手动安装,你可以从Windows 2000的system32中...

    自动发送邮件代码!

    自动发送邮件代码,自己测试成功!需要的朋友可以借鉴一下!

    C# 发送邮件代码 C# 发送邮件代码

    根据给定的信息,本文将详细解释如何使用C#语言实现发送带有附件的电子邮件的功能,并针对提供的代码进行深入解析。 ### C#发送邮件的基础概念 在.NET框架中,发送电子邮件主要依赖于`System.Net.Mail`命名空间下...

    delphi发邮件代码

    标题 "Delphi发邮件代码" 描述的是一个使用Delphi编程语言编写的电子邮件发送和接收应用程序。这个程序可能包含了一个用户界面,允许用户输入邮件信息,包括收件人、主题和正文,并能发送附件。从描述中我们可以推断...

    asp发送邮件代码

    asp发送邮件代码,微软自带CDONTS组件发送核心代码,Jmail程序调用,w3 Jmail4.3组件核心代码.

    SMTP发送邮件代码和解释说明

    SMTP 发送邮件代码和解释说明 在本文中,我们将详细介绍如何使用 SMTP 协议发送邮件,包括邮件的基本结构、邮件发送的步骤、SMTP 客户端的使用、邮件附件的添加等相关知识点。 SMTP 协议简介 SMTP(Simple Mail ...

    android发送邮件代码

    以下将详细讲解如何在Android应用中编写代码来发送邮件。 首先,你需要在Android项目中引入JavaMail API的相关库。虽然Android系统本身并不直接支持JavaMail,但可以通过第三方库如Android JavaMail Library(如`...

    VBS 发送邮件代码

    VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码VBS发邮件代码

    Matlab 发送邮件代码

    利用matlab发送定制的信息邮件,以便在跑大量数据时,可以通过提醒! function Sendmail Answer = 1; %判断条件 if Answer>0.5 % 账号设置 mail = '********@qq.com'; % ①邮箱地址 password = '******'; % ②...

    简单的发送邮件代码

    简单的发送邮件代码,使用delphi编辑实现,欢迎大家使用

    vb 发邮件代码

    vb 发邮件代码,利用vb自带的控件发邮件。可以用于程序内置邮件程序,不过必须有outlook

    C#发送邮件

    `MailMessage` 类用于构建邮件信息,包括发件人、收件人、抄送人、密送人、主题和邮件正文。以下是如何创建一个简单的MailMessage实例: ```csharp MailMessage message = new MailMessage(); message.From = ...

    delphi发送邮件代码

    根据提供的Delphi发送邮件代码,我们可以总结出以下关键知识点: ### 1. 使用 IdSMTP 组件进行邮件发送 #### 1.1 初始化 IdSMTP 组件 - `IdSMTP1.AuthenticationType := atLogin;`:设置身份验证类型为登录验证。 ...

    java mail 发送邮件代码与架包

    在标题和描述中提到的"java mail 发送邮件代码",通常涉及以下几个关键知识点: 1. **JavaMail API**: 这是Java中用来处理邮件的核心库,包括`javax.mail`和`javax.mail.internet`包。你需要导入这些包来使用其提供...

    sendemai(PHP发送邮件代码).doc

    根据提供的文档信息,本文将详细解析“sendemai(PHP发送邮件代码)”中的关键知识点,包括PHP如何通过SMTP协议实现电子邮件的发送过程。 ### 标题分析:“sendemai(PHP发送邮件代码)” 此标题直接表明了文档的主要...

    阿里企业邮箱发送邮件代码

    C#、ASP.NET 完整版 阿里企业邮箱发送邮件代码,代码清晰易懂,简洁明了,只需要配置自己的信息即可使用!!!

    VC控制台发送邮件代码

    这个"VC控制台发送邮件代码"是一个基于C++语言的控制台项目,旨在演示如何利用SMTP(Simple Mail Transfer Protocol)协议来发送电子邮件。SMTP是互联网上用于传输电子邮件的标准协议。在这个案例中,开发者使用了一...

Global site tag (gtag.js) - Google Analytics