java发送email(含代理方式,ssl方式,传统方式)

package spring.vhostall.com;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailTest {
/**
 * 通过代理发送邮件
 */
    /**
      * @param args
     */
    public static void main(String[] args) throws Exception {
     MailTest test = new MailTest();
     //发送邮件
//     test.sendmail();
     //通过代理发送邮件
     test.sendMailByProxy();
     }

    private void sendMailByProxy()throws Exception{
          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
     //设置代理服务器
      Properties props = System.getProperties();
      props.setProperty("proxySet", "true");
      props.setProperty("socksProxyHost", "43.82.112.214");
      props.setProperty("socksProxyPort", "1080");
      props.setProperty("mail.smtp.host", "smtp.163.com");
      props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.setProperty("mail.smtp.socketFactory.fallback", "false");
      props.setProperty("mail.smtp.port", "465");
      props.setProperty("mail.smtp.socketFactory.port", "465");
      props.setProperty("mail.smtp.auth", "true");
      props.setProperty("mail.debug", "true");
      props.setProperty("mail.transport.protocol", "smtp");
     final String username = "****";
     final String password = "****";

     //使用验证
      Session session = Session.getDefaultInstance(props,
             new javax.mail.Authenticator() {
                 protected PasswordAuthentication getPasswordAuthentication() {
                     return new javax.mail.PasswordAuthentication(username,password);
                  }
              });
      MimeMessage message = new MimeMessage(session);
      Address address = new InternetAddress("****");
      Address toAaddress = new InternetAddress("****");

      message.setFrom(address);
      message.setRecipient(MimeMessage.RecipientType.TO, toAaddress);
      message.setSubject("测试");
      message.setText("test");
      message.setSentDate(new Date());
      Transport transport = session.getTransport();
      transport.connect("smtp.163.com","****","****");
      transport.sendMessage(message, new Address[] {new InternetAddress("****")});
//      Transport.send(message);
      System.out.println("邮件发送!");
      transport.close();

    }
 public void sendmail() {/*
  try {
   String host = "smtp.gmail.com";
   String from = "[email protected]";
   String to = "@163.com";
   String username = "****";
   String password = "****";
   String body = "This is a test";
   String subject = "Email Test";
   SmtpAuth sa = new SmtpAuth();
   sa.getuserinfo(username, password);
   Session session;
   MimeMessage message;
   Properties props = System.getProperties();
   props.setProperty("proxySet", "true");
   props.setProperty("socksProxyHost", "43.82.218.50");
   props.setProperty("socksProxyPort", "8080");
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.host", host);
   session = Session.getInstance(props, sa);
   session.setDebug(true);
   message = new MimeMessage(session);
   message.setFrom(new InternetAddress(from));
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(
     to));
   message.setSubject(subject);
   MimeBodyPart mbp = new MimeBodyPart();
   mbp.setText(body);
   Multipart mp = new MimeMultipart();
   mp.addBodyPart(mbp);
   message.setContent(mp);
   message.setSentDate(new java.util.Date());
   Transport transport = session.getTransport("smtp");
   transport.connect(host, username, password);
   transport.send(message);
  } catch (MessagingException e) {
   System.out.println("error" + e.getMessage());
  }
 */}

 class SmtpAuth extends javax.mail.Authenticator {
  private String user, password;

  public void getuserinfo(String getuser, String getpassword) {
   user = getuser;
   password = getpassword;
  }

  protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
   return new javax.mail.PasswordAuthentication(user, password);
  }
 }
}

依赖的jar包:http://download.csdn.net/detail/wu858773457/8699111

时间: 2024-10-03 21:15:34

java发送email(含代理方式,ssl方式,传统方式)的相关文章

java 发送 Email

private static void sendMail(String mail, String mailContext) { try { String context = FileUtil.getFileContent("lostpassEmail.html"); HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.163.com"); email.setSmtpPort(25); email.se

CentOS7.3将网卡命名方式设置为传统方式

CentOS7.3将网卡命名方式设置为传统方式 生产环境可能拥有不同系列的操作系统,比如,既有CentOS6系列,也有CentOS7系列的系统,而CentOS6和CentOS7在网卡命名方面有着较大区别,为便于管理,可以考虑将CentOS7的命名方式设置为传统的命名方式. 操作步骤 1. 编辑/etc/default/grub配置文件 GRUB_CMDLINE_LINUX="rhgbquiet net.ifnames=0" 或:修改/boot/grub2/grub.cfg 2.   为

java发送email

package com.assess.util; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Se

Java发送Email—使用org.apache.commons.mail

上篇文章介绍了自己手动的实现发送普通Email.HTML类型的Email以及带附件的Email.其实apache的commons项目下有个email子项目,它对JavaMail API进行了封装,用起来特变方便.下面就简单介绍. 1.    首先配置需要的jar包 需要将mail.jar和commons-email.jar添加到我们的CLASSPATH中即可,如下图: 2.    发送普通邮件 /** * 用org.apache.commons.mail发送普通邮件 * * @author wa

java发送email文件

1. 加入jar包 <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency> 2.编码如下: 方式1: import java.util.Date; import java.util.Properties; import javax.mai

关于java发送email案例

1.先创建一个Email所用到的信息类: package com.tan.test; import java.util.Properties; /** * 功能:邮箱信息类 * @author TanZi * @time 2015-4-20上午10:35:47 */ public class MailSenderInfo { //发送邮件的服务器验证需要的信息 private String mailServerHost; //服务器的IP地址 private String mailServerP

Java动态代理的两种实现方式:

方式一:传统的代理 package cn.hc.domain; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * 代理对象创建的工厂类 * @author hc * */ public class JDKProxyFactory implements InvocationHandler{ //被代理对象 private

[Python] 发送email的几种方式

python发送email还是比较简单的,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,还是抄送都比较容易实现. 先把几个最简单的发送邮件方式记录下,像html邮件,附件等也是支持的,需要时查文档即可 1 登录邮件服务 #!/usr/bin/env python # -*- coding: utf-8 -*- #python2.7x #send_simple_email_by_account.

java邮件发送(含附件)

1. [代码]java邮件发送(含附件)疯狂的IT人站长整理的:利用Java发送邮件(含附件)的例子:1.邮件发送的配置propertity文件内容如下:(utils.properties文件放在src下面)emailsmtp=smtp.qq.com[email protected]emailpass=******2.读取配置文件的类文件(ReadPropertity.java)import java.io.IOException;import java.util.Properties;publ