A javabean that sends mail

Source: Internet
Author: User
/*
*author:tyfun
*datetime:2003.01.09
*package:com.westarsoft.function
*/

Package com.westarsoft.function;

Import java.util.*;
Import java.io.*;
Import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;

public class SendMail {
private String smtpserver = new string ();
private string from = new String ();
private String subject = new String ();
private string content = new string ();
private String user = new String ();
private String Password = new string ();
Public String Getsmtpserver () {
return smtpserver;
}
public void Setsmtpserver (String smtpserver) {
This. SmtpServer = SmtpServer;
}
Public String Getfrom () {
return from;
}
public void Setfrom (String from) {
This.from = from;
}
Public String Getsubject () {
return subject;
}
public void Setsubject (String subject) {
This.subject = subject;
}
Public String getcontent () {
return content;
}
public void SetContent (String content) {
this.content = content;
}
Public String GetUser () {
return user;
}
public void SetUser (String user) {
This.user = user;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}

public void Sendmailto (String to) {
try {
Properties Props = new properties ();
Session sendmailsession;
Store store;
Transport transport;
      
Sendmailsession = session.getinstance (props, null);
Props.put ("Mail.smtp.host", smtpserver);
MimeMessage newmessage = new MimeMessage (sendmailsession);
Newmessage.setfrom (New InternetAddress (from));
Newmessage.setsubject (subject);
Newmessage.setsentdate (New Date ());
Newmessage.settext (content);

StringTokenizer token = new StringTokenizer (To, ",");
internetaddress[] Addrarr = new Internetaddress[token.counttokens ()];
int i = 0;
while (Token.hasmoretokens ()) {
Addrarr[i] = new InternetAddress (Token.nexttoken (). toString ());
i++;
}

Transport = Sendmailsession.gettransport ("SMTP");
Transport.connect (smtpserver, user, password);
Newmessage.setrecipients (Message.RecipientType.TO, Addrarr);
Transport.send (Newmessage);
}
catch (Exception e) {
System.out.println (e);
}
}
  
/*
public static void Main (string[] args) {
SendMail mail = new SendMail ();
Mail.setsmtpserver ("200.1.1.157");
Mail.setuser ("lint");
Mail.setpassword ("30320");
Mail.setfrom ("lint@westar.com");
Mail.setsubject ("test");
Mail.setcontent ("test");
Mail.sendmailto ("lint@westar.com,zouy@westar.com,zhangp@westar.com");
}
*/
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.