`
solojava
  • 浏览: 21639 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

google apps 企业邮箱 发送 代码

阅读更多
注意需要最新的mail.jar

package xueyuan.commons;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-2-15
* Time: 14:02:18
* To change this template use File | Settings | File Templates.
*/
import java.io.PrintStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.*;
import javax.mail.*;

public class SendMail
{
private MimeMessage mimeMsg;
  private Session session;
  private Properties props;
  private boolean needAuth;
  private String username;
  private String password;
  private Multipart mp;
    
public SendMail(String smtp)
{
     needAuth = false;
     username = "";
     password = "";
     setSmtpHost(smtp);
     createMimeMessage();
}

public void setSmtpHost(String hostName)
{
     System.out.println("设置系统属性:mail.smtp.host = " + hostName);
     if(props == null)
         props = System.getProperties();
     props.put("mail.smtp.host", hostName);
//     System.out.println(props.get("mail.smtp.host"));
     //gmail需要的协议
     props.put("mail.smtp.socketFactory.class",  "javax.net.ssl.SSLSocketFactory");
//     System.out.println(props.get("mail.smtp.socketFactory.class"));
     //gmail特殊端口
     props.put("mail.smtp.port","465");
}

public boolean createMimeMessage()
{
     try
     {
         System.out.println("准备获取邮件会话对象!");
         PopupAuthenticator popAuthenticator = new PopupAuthenticator();
         PasswordAuthentication   pop  =  popAuthenticator.performCheck(username,password);
         session = Session.getDefaultInstance(props, popAuthenticator);
     }
     catch(Exception e)
     {
         System.err.println("获取邮件会话对象时发生错误!" + e);
//         e.printStackTrace();
         return false;
     }
     System.out.println("准备创建MIME邮件对象!");
     try
     {
         mimeMsg = new MimeMessage(session);
         mp = new MimeMultipart();
     }
     catch(Exception e)
     {
         System.err.println("创建MIME邮件对象失败!" + e);
         //         e.printStackTrace();
         return false;
     }
     return true;
}

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");
}

public void setNamePass(String name, String pass)
{
//     System.out.println("程序得到用户名与密码");
     username = name;
     password = pass;
}

public boolean setSubject(String mailSubject)
{
//     System.out.println("设置邮件主题!");
     try
     {
         mimeMsg.setSubject(mailSubject);
     }
     catch(Exception e)
     {
//         System.err.println("设置邮件主题发生错误!");
         return false;
     }
     return true;
}

public boolean setBody(String mailBody)
{
     try
     {
//         "设置邮件体格式"
         BodyPart bp = new MimeBodyPart();
         bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + mailBody, "text/html;charset=GB2312");
         mp.addBodyPart(bp);
     }
     catch(Exception e)
     {
         System.err.println("设置邮件正文时发生错误!" + e);
         return false;
     }
     return true;
}

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);
     }
     catch(Exception e)
     {
         System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
         return false;
     }
     return true;
}

public boolean setFrom(String from)
{
     System.out.println("设置发信人!");
     try
     {
         mimeMsg.setFrom(new InternetAddress(from));
     }
     catch(Exception e)
     {
         return false;
     }
     return true;
}

public boolean setTo(String to)
{
     System.out.println("设置收信人");
     if(to == null)
         return false;
     try
     {
         mimeMsg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(to));
     }
     catch(Exception e)
     {
         return false;
     }
     return true;
}

public boolean setCopyTo(String copyto)
{
     System.out.println("发送附件到");
     if(copyto == null)
         return false;
     try
     {
         mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC, InternetAddress.parse(copyto));
     }
     catch(Exception e)
     {
         return false;
     }
     return true;
}

public boolean sendout()
{
     try
     {
         mimeMsg.setContent(mp);
         mimeMsg.saveChanges();
         System.out.println("正在发送邮件....");
         PopupAuthenticator popAuthenticator = new PopupAuthenticator();
         PasswordAuthentication   pop  =  popAuthenticator.performCheck(username,password);
         Session mailSession = Session.getInstance(props,popAuthenticator);
         //协议种类
         Transport transport = mailSession.getTransport("smtp");
         transport.connect((String)props.get("mail.smtp.host"), username, password);
         transport.sendMessage(mimeMsg, mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO));
         System.out.println("发送邮件成功!");
         transport.close();
     }
     catch(Exception e)
     {
         System.err.println("邮件发送失败!" + e.getMessage());
//         e.printStackTrace();
         return false;
     }
     return true;
}

    public static void main(String[] args) {
    // TODO 自动生成方法存根
     SendMail themail = new SendMail("smtp.gmail.com");
        String mailbody = "尊敬的" ;
            themail.setNeedAuth(true);
//邮件的title
            themail.setSubject("找回密码");
//邮件的内容
            themail.setBody(mailbody);
//发给谁
            themail.setTo("it@yearbookcn.com");
//从哪个邮件发与下面的setNamepass的需要一致
            themail.setFrom("xxx@yearbookcn.com");
            themail.setNamePass("xxx@yearbookcn.com","***");
            themail.sendout();
}
}

调用的方法(也就是邮件的验证)

package xueyuan.commons;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-2-15
* Time: 15:23:08
* To change this template use File | Settings | File Templates.
*/
import javax.mail.*;
import javax.mail.internet.*;

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);
         }

}

分享到:
评论

相关推荐

    GoogleAppsScript, Google Apps Script的示例代码.zip

    GoogleAppsScript, Google Apps Script的示例代码 GoogleAppsScriptGoogle Apps Script的示例代码这些不是官方的Google示例。 这些是支持演示和其他演示的概念的证明。请将这里代码用作 guidline,而不要作为生产...

    Google+Apps+Script.pdf

    总之,《Google Apps Script》第二版是一本全面的指南,适合从初学者到有经验的开发者,无论你是为了个人还是企业的Google应用程序增强功能,这本书都将提供详尽的指导和实用的示例。通过学习,你可以掌握使用...

    google apps script源码参考

    Google Apps Script是一种基于JavaScript的云编程平台,专为构建Google应用程序(如Gmail、Google Sheets、Google Docs等)的自定义功能和自动化任务而设计。它允许用户通过编写脚本来扩展这些应用的功能,无需具备...

    Google Apps Script,第2版

    ### Google Apps Script 第二版 —— 关键知识点详解 #### 标题与描述解析 - **标题**: "Google Apps Script, 第2版" - **核心概念**: 本书是关于Google Apps Script的第二版,主要介绍了如何利用Google Apps ...

    [Google] Google Apps Script 第2版 (英文版)

    Learn how to create dynamic web applications with Google Apps Script and take full advantage of your Google-hosted services. If you have basic coding skills and some JavaScript experience, this ...

    谷歌 apps插件开发实例

    谷歌Apps插件,也称为Chrome Apps,是一种基于Web技术构建的桌面应用程序,它们可以在Google Chrome浏览器上运行,提供类似于原生应用的体验。Chrome Apps利用HTML、CSS和JavaScript等Web技术,结合Chrome的API扩展...

    Learning Google Apps Script(PACKT,2016)

    Google Apps Script is a cloud-based scripting language based on JavaScript to customize and automate Google applications. Apps Script makes it easy to create and publish add-ons in an online store ...

    Google Apps Sync For Outlook

    googleappssyncsetup.exe ,这个是在线安装包,包含了Google Apps Migration For Microsoft Outlook® 和 google Apps sync for outlook; 运行这个软件. 运行这个在线安装包,能安装好google apps migration for ...

    Learning.Google.Apps.Script.178588

    Google Apps Script is a cloud-based scripting language based on JavaScript to customize and automate Google applications. Apps Script makes it easy to create and publish add-ons in an online store ...

    申请和配置Google Apps邮件服务器,附带JAVA MAIL邮件发送系统

    标题中的“申请和配置Google Apps邮件服务器”涉及的是如何注册并设置Google提供的企业级应用套件,即Google Workspace(以前称为Google Apps),用于管理和发送电子邮件。Google Workspace提供了专业的电子邮件服务...

    谷歌免费短信 在线发送 PHP

    1. **谷歌应用**:谷歌应用(Google Apps)是谷歌提供的一系列在线协作工具集合,包括Gmail、日历、文档、Drive等,企业或个人可以使用这些工具构建自己的工作环境。这里的“谷歌应用”可能是指谷歌的云服务或API...

    Google Apps Script(2nd) epub

    Google Apps Script(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Google Apps Hacks Apr 2008

    Google Apps包括Gmail、Google Calendar、Google Docs、Google Drive等组件,为企业和个人提供了高效、协作的工作环境。下面我们将深入探讨这些技巧和黑客方法,揭示如何更有效地使用Google Apps。 1. Gmail Hacks...

    Google Apps Security Whitepaper 06_10_2010.pdf

    ### 安全白皮书概览:Google Apps消息传递与协作产品 #### 引言 随着第三方托管服务的不断扩展,以及近年来“云计算”概念的兴起和发展,企业对在线服务安全性的关注日益增加。这份白皮书《Google Apps Security ...

    stripe-apps-script:Google Apps Script 中 Stripe API 的示例代码

    这个名为 "stripe-apps-script" 的项目,就是将 Stripe API 与 Google Apps Script 结合使用的示例代码库。 **主要知识点:** 1. **Google Apps Script 基础**: - Google Apps Script 是一种服务器端的 ...

    Apps-Script:Apps 脚本代码

    应用脚本Apps 脚本代码此代码主要用于在导师和学员通过填写各自的谷歌表格进行注册后自动发送邮件,然后代码将从可用导师列表中搜索(导师可能会提到他们的学员能力),然后分配每个一名导师。 有关各自导师或受训者...

    Android Apps Security书籍配套源代码

    《Android Apps Security》是一本专注于Android应用安全的书籍,其配套源代码可以在GitHub上找到,链接为&lt;https://github.com/apress/android-apps-security&gt;。这个压缩包文件名为"android-apps-security-master",...

    sendmails_from_googlespreadsheet:简单的Google Apps脚本,可以自动从Google Spreadsheet中的电子邮件地址列表发送单个邮件

    【标题】:使用Google Apps Script实现自动化邮件发送 在标题中提到的"sendmails_from_googlespreadsheet"是一个基于Google Apps Script的项目,它允许用户从Google Sheets中的电子邮件地址列表批量发送个性化邮件...

    Asp.Net 发送邮件(Email) 实例 代码 亲测 可用.rar

    此外,某些SMTP服务器可能需要启用“Less Secure Apps”或使用应用程序特定的授权代码才能从非本地环境发送邮件。这在使用Google Gmail或其他类似服务时尤其常见。 总之,Asp.Net发送邮件功能通过结合SmtpClient和...

Global site tag (gtag.js) - Google Analytics