Gmail.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:4k
- /*
- * Created on 2005-5-20
- * Made In GamVan.com
- * 在线发信的Bean
- */
- package com.gamvan.tools;
- import java.security.Security;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.activation.DataSource;
- public class Gmail {
- private String toEmail2 = ""; //抄送
- private String toName=""; //接收人
- private String toEmail=""; //接收人邮件地址
- private String fromName=""; //发件人
- private String fromEmail=""; //发件人邮件地址
- //private String filename="";
- private String subject=""; //邮件标题
- private String content=""; //邮件内容
- private String smtpServer="";
- private String smtpID="";
- private String smtpPass="";
- //private String popServer = "";
- private String fileName="";
- private boolean needAuth = false;
- private String message = "";
- private int needSSL = 0;
- private String smtpPort = "";
- //private String popPort = "";
-
- public void setToEmail2(String toEmail2) {
- this.toEmail2 = toEmail2;
- }
- public String getMessage(){
- return this.message;
- }
- public void setSmtpPort(String s){
- this.smtpPort = s;
- }
- //public void setPopPort(String s){
- //this.popPort = s;
- //}
- public void setNeedSSL(int i){
- this.needSSL = i;
- }
- public void setToEmail(String toEmail){
- this.toEmail = toEmail;
- }
- public void setToName(String toName){
- this.toName = toName;
- }
- public void setFromName(String fromName){
- this.fromName = fromName;
- }
- public void setFromEmail(String fromEmail){
- this.fromEmail = fromEmail;
- }
- public void setSmtpServer(String smtpServer){
- this.smtpServer = smtpServer;
- }
- public void setSmtpID(String smtpID){
- this.smtpID = smtpID;
- }
- public void setSmtpPass(String smtpPass){
- this.smtpPass = smtpPass;
- }
- public void setSubject(String subject){
- this.subject = subject;
- }
- public void setContent(String content){
- this.content = content;
- }
- public void setNeedAuth(String needAuth){
- try{
- this.needAuth = Boolean.parseBoolean(needAuth);
- }catch(Exception e){
- this.needAuth = true;
- }
- }
-
- //发送方法
- public boolean mailSender(){
-
- boolean bea = false;
- //Properties
- Properties props = System.getProperties();
- props.setProperty("mail.smtp.host", smtpServer);
- if(needSSL==1){ //判断是否有SSL安全套接字验证
- Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
- final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
- props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
- props.setProperty("mail.smtp.socketFactory.fallback", "false");
- }
- if(!smtpPort.equals("")){ //smtp服务器端口
- props.setProperty("mail.smtp.port", smtpPort);
- props.setProperty("mail.smtp.socketFactory.port", smtpPort);
- }
- if(needAuth){ //判断SMTP是否需要身份验证
- props.setProperty("mail.smtp.auth","true");
- }else{
- props.setProperty("mail.smtp.auth","false");
- }
- //
- ///Session session=Session.getDefaultInstance(props, null);
- Session session = Session.getDefaultInstance(props, new Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(smtpID, smtpPass);
- }});
-
- try {
- //
- MimeMessage msg = new MimeMessage(session);
- InternetAddress address = new InternetAddress(fromEmail);
- msg.setFrom(address);
- msg.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(toEmail,false));
-
- if(toEmail2!=null && !toEmail2.equals("")){
- msg.setRecipients(Message.RecipientType.CC,
- InternetAddress.parse(toEmail2,false));
- }
- msg.setSubject(subject); //设置邮件标题
- msg.setText(content);//设置邮件内容
- msg.setSentDate(new Date()); //邮件发送日期
- Transport.send(msg); //执行发送
- message = "Email发送成功......";
- bea = true;
- }catch(MessagingException e){
- message = e.toString();
- bea = false;
- }finally{
- }
- return bea;
- }
- }