java Mailer

 import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/** Mailer.  Sends an email message.
 * Sender class, recast as a Bean for use in JSP's & elsewhere.
 * Example usage:
 * <pre>
 * Mailer mb = new Mailer();
 * mb.setFrom("sending email address");
 * mb.addTo("destination email address");
 * mb.setSubject("the subject string");
 * mb.setBody(body.toString());
 * mb.setServer(application.getInitParameter("mail.server.smtp"));
 * try {
 *     mb.doSend();
 * } catch (MessagingException ex) {
 *    ...
 * }
 * </pre>
 *
 * @version $Id: Mailer.java,v 1.0 2006/07/23 01:26:05 cn Exp $
 */
public class Mailer {
 /** The javamail session object. */
 protected Session session;
 /** The sender's email address */
 protected String from;
 /** The subject of the message. */
 protected String subject;
 /** The recipient ("To:"), as Strings. */
 protected ArrayList toList = new ArrayList();
 /** The CC list, as Strings. */
 protected ArrayList ccList = new ArrayList();
 /** The BCC list, as Strings. */
 protected ArrayList bccList = new ArrayList();
 /** The text of the message. */
 protected String body;
 /** The SMTP relay host */
 protected String mailHost;
 /** The sender's name */
 protected String sName;
 /** The sender's password */
 protected String sPassword;
 /** The verbosity setting */
 protected boolean verbose;

 /** Get from */
 public String getFrom() {
  return from;
 }

 /** Set from */
 public void setFrom(String fm) {
  from = fm;
 }

 /** Get subject */
 public String getSubject() {
  return subject;
 }

 /** Set subject */
 public void setSubject(String subj) {
  subject = subj;
 }

 // SETTERS/GETTERS FOR TO: LIST

 /** Get tolist, as an array of Strings */
 public ArrayList getToList() {
  return toList;
 }

 /** Set to list to an ArrayList of Strings */
 public void setToList(ArrayList to) {
  toList = to;
 }

 /** Set to as a string like "tom, mary, robin@host". Loses any
  * previously-set values. */
 public void setToList(String s) {
  toList = tokenize(s);
 }

 /** Add one "to" recipient */
 public void addTo(String to) {
  toList.add(to);
 }

 // SETTERS/GETTERS FOR CC: LIST

 /** Get cclist, as an array of Strings */
 public ArrayList getCcList() {
  return ccList;
 }

 /** Set cc list to an ArrayList of Strings */
 public void setCcList(ArrayList cc) {
  ccList = cc;
 }

 /** Set cc as a string like "tom, mary, robin@host". Loses any
  * previously-set values. */
 public void setCcList(String s) {
  ccList = tokenize(s);
 }

 /** Add one "cc" recipient */
 public void addCc(String cc) {
  ccList.add(cc);
 }

 // SETTERS/GETTERS FOR BCC: LIST

 /** Get bcclist, as an array of Strings */
 public ArrayList getBccList() {
  return bccList;
 }

 /** Set bcc list to an ArrayList of Strings */
 public void setBccList(ArrayList bcc) {
  bccList = bcc;
 }

 /** Set bcc as a string like "tom, mary, robin@host". Loses any
  * previously-set values. */
 public void setBccList(String s) {
  bccList = tokenize(s);
 }

 /** Add one "bcc" recipient */
 public void addBcc(String bcc) {
  bccList.add(bcc);
 }

 // SETTER/GETTER FOR MESSAGE BODY

 /** Get message */
 public String getBody() {
  return body;
 }

 /** Set message */
 public void setBody(String text) {
  body = text;
 }
 
 // SETTER/GETTER FOR sender's name

 /** Get sender's name */
 public String getsName() {
  return sName;
 }

 /** Set sender's name */
 public void setsName(String name) {
  sName = name;
 }
 
 // SETTER/GETTER FOR sender's password

 /** Get sender's password */
 public String getsPassword() {
  return sPassword;
 }

 /** Set sender's password */
 public void setsPassword(String password) {
  sPassword = password;
 } 
 
 // SETTER/GETTER FOR VERBOSITY

 /** Get verbose */
 public boolean isVerbose() {
  return verbose;
 }

 /** Set verbose */
 public void setVerbose(boolean v) {
  verbose = v;
 }

 /** Check if all required fields have been set before sending.
  * Normally called e.g., by a JSP before calling doSend.
  * Is also called by doSend for verification.
  */
 public boolean isComplete() {
  if (from == null    || from.length()==0) {
   System.err.println("doSend: no FROM");
   return false;
  }
  if (subject == null || subject.length()==0) {
   System.err.println("doSend: no SUBJECT");
   return false;
  }
  if (toList.size()==0) {
   System.err.println("doSend: no recipients");
   return false;
  }
  if (body == null || body.length()==0) {
   System.err.println("doSend: no body");
   return false;
  }
  if (mailHost == null || mailHost.length()==0) {
   System.err.println("doSend: no server host");
   return false;
  }
  return true;
 }

 public void setServer(String s) {
  mailHost = s;
 }

 /** Send the message.
  */
 public synchronized void doSend() throws MessagingException {

  if (!isComplete())
   throw new IllegalArgumentException(
    "doSend called before message was complete");

  /** Properties object used to pass props into the MAIL API */
  Properties props = new Properties();
  props.put("mail.smtp.host", mailHost);
  props.put("mail.smtp.auth", "true");

  // Create the Session object
  if (session == null) {
   session = Session.getDefaultInstance(props, null);
   if (verbose)
    session.setDebug(true);  // Verbose!
  }
  
  // create a message
  final Message mesg = new MimeMessage(session);

  InternetAddress[] addresses;

  // TO Address list
  addresses = new InternetAddress[toList.size()];
  for (int i=0; i<addresses.length; i++)
   addresses[i] = new InternetAddress((String)toList.get(i));
  mesg.setRecipients(Message.RecipientType.TO, addresses);

  // From Address
  mesg.setFrom(new InternetAddress(from));

  // CC Address list
  addresses = new InternetAddress[ccList.size()];
  for (int i=0; i<addresses.length; i++)
   addresses[i] = new InternetAddress((String)ccList.get(i));
  mesg.setRecipients(Message.RecipientType.CC, addresses);

  // BCC Address list
  addresses = new InternetAddress[bccList.size()];
  for (int i=0; i<addresses.length; i++)
   addresses[i] = new InternetAddress((String)bccList.get(i));
  mesg.setRecipients(Message.RecipientType.BCC, addresses);

  // The Subject
  mesg.setSubject(subject);

  // Now the message body.
  mesg.setText(body);

  // Finally, send the message! (use static Transport method)
  // Uncomment to do this in a Thread if you like to speed up
  // new Thread() {
   // public void run() {
    // try {
//  Transport.send(mesg);
  Transport trans = session.getTransport("smtp");
  trans.connect(mailHost, sName, sPassword);
  trans.sendMessage(mesg, mesg.getAllRecipients());

    // } catch (MessagingException e) {
     // throw new IllegalArgumentException(
     // "Transport.send() threw: " + e.toString());
    // }
   // }
  // }.start();
 }

 /** Convenience method that does it all with one call.
  * @param mailhost - SMTP server host
  * @param recipient - domain address of email (user@host.domain)
  * @param sender - your email address
  * @param subject - the subject line
  * @param message - the entire message body as a String with embedded /n's
  */
 public static void send(String mailhost,
  String recipient, String sender, String subject, String message)
 throws MessagingException {
  Mailer m = new Mailer();
  m.setServer(mailhost);
  m.addTo(recipient);
  m.setFrom(sender);
  m.setSubject(subject);
  m.setBody(message);
  m.doSend();
 }


 /** Convert a list of addresses to an ArrayList. This will work
  * for simple names like "tom, mary@foo.com, 123.45@c$.com"
  * but will fail on certain complex (but RFC-valid) names like
  * "(Doe, John) <http://www.johndoe.com/>".
  * Or even "John Doe <http://www.johndoe.com/>".
  */
 protected ArrayList tokenize(String s) {
  ArrayList al = new ArrayList();
  StringTokenizer tf = new StringTokenizer(s, ",");
  // For each word found in the line
  while (tf.hasMoreTokens()) {
   // trim blanks, and add to list.
   al.add(tf.nextToken().trim());
  }
  return al;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要安装 PHPMailer,请按以下步骤进行操作: 1. 下载 PHPMailer 您可以从 PHPMailer 的官方网站上下载最新版本的 PHPMailer,也可以使用 Composer 安装。在本教程中,我们将使用第一种方法。 2. 解压缩 PHPMailer 将下载的 PHPMailer 压缩文件解压缩到您的 Web 服务器上。 3. 包含 PHPMailer 在您的 PHP 文件中包含 PHPMailer 类。例如,如果您将 PHPMailer 解压缩到您的 Web 服务器的根目录中,则可以使用以下命令包含 PHPMailer: require_once '/path/to/PHPMailer/PHPMailerAutoload.php'; 4. 配置 PHPMailer 在 PHPMailer 中配置您的 SMTP 设置和其他选项。例如,如果您使用 Gmail SMTP,您可以使用以下代码: $mail->isSMTP(); // 使用 SMTP $mail->Host = 'smtp.gmail.com'; // 您的 SMTP 服务器地址 $mail->SMTPAuth = true; // 启用 SMTP 认证 $mail->Username = '[email protected]'; // 发件人邮箱地址 $mail->Password = 'your.email.password'; // 发件人邮箱密码 $mail->SMTPSecure = 'tls'; // 启用 TLS 加密,也可以使用 SSL 加密 $mail->Port = 587; // SMTP 端口号 5. 发送邮件 使用 PHPMailer 发送电子邮件。例如,以下代码将发送一封电子邮件: $mail = new PHPMailer(); // 实例化 PHPMailer $mail->setFrom('[email protected]', 'Your Name'); // 设置发件人邮箱地址和名称 $mail->addAddress('[email protected]', 'Recipient Name'); // 添加收件人邮箱地址和名称 $mail->Subject = 'Subject'; // 设置邮件主题 $mail->Body = 'Content'; // 设置邮件正文 if(!$mail->send()) { echo 'Error: ' . $mail->ErrorInfo; // 发送失败 } else { echo 'Message sent.'; // 发送成功 } 以上就是 PHPMailer 的安装过程。请注意,在生产环境中使用 PHPMailer 时,您应该采取一些安全措施,如使用 SMTP 认证和加密,以确保电子邮件的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值