| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

JavaMail发送简单邮件代码

package cn.jmail.test;     import java.util.Properties;     import javax.mail.*;  import javax.mail.internet.*;     public class FirstMail {      /**       * 发送简单邮件方法       * @param host    发送邮件服务器的IP       * @param from    发送人地址       * @param to    接收人地址       * @param subject    邮件主题       * @param text    内容       * @param senderUsername    发送人的账户       * @param senderPassword    发送人的密码       * mail.smtp.auth 是否需要身份验证 一般都是需要的       */      public static void sendMail(String host, String from, String to, String subject, String text,               final String senderUsername, final String senderPassword){          Properties props = System.getProperties();          props.put("mail.smtp.host", host);          props.put("mail.smtp.auth", "true");          Session session = Session.getDefaultInstance(props, new Authenticator() {              @Override              public PasswordAuthentication getPasswordAuthentication(){                  return new PasswordAuthentication(senderUsername, senderPassword);              }          });          try {              MimeMessage message = new MimeMessage(session);              message.setFrom(new InternetAddress(from));              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));              message.setSubject(subject);              message.setText(text);              Transport.send(message);          } catch (AddressException e) {              e.printStackTrace();          } catch (MessagingException e) {              e.printStackTrace();          }      }             public static void main(String[] args) {          String host = "smtp.163.com";          String from = "xxxxxx@163.com";          String to = "xxxxxxxx@qq.com";          String subject = "Hello, this is a test email.";          String text = "Hello,LiLei.";           String senderUsername = "xxxxxx@163.com";          String senderPassword = "xxxxxx";          FirstMail.sendMail(host, from, to, subject, text, senderUsername, senderPassword);      }  }

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1413361392856.html
邮件 Java开发