Using Java to complete the sending of mail source via SMTP

Source: Internet
Author: User

SOURCE Download Address

Package com.in.smtp;
Import javax.mail.*;

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


public class Emailforsmtp {
String host = "";
String username = "";
String password = "";


public void Sethost (String host) {
This.host = host;
}

public void setuserinformaition (String username,string password) {
This.username = Username;
This.password = password;
}

public void SendEmail (String sendpeople,string arrviedpeople,string emailname,string content) {
Properties Props = new properties ();
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", true);

try {
Session mailsession = session.getdefaultinstance (props);
Mailsession.setdebug (TRUE);
Message msg = new MimeMessage (mailsession);
Msg.setfrom (New InternetAddress (sendpeople));
Msg.addrecipient (message.recipienttype.to,new internetaddress (arrviedpeople));//Recipient
Msg.setsubject (emailname);//Mail subject
Msg.settext (content);//Message contents
Msg.savechanges ();

Transport transport = Mailsession.gettransport ("SMTP");
Transport.connect (host, username, password);
Transport.sendmessage (MSG, msg.getallrecipients ());
Transport.close ();
catch (Addressexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchproviderexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Messagingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
}

--------------------------------------------------------------------------------------------------------------- ---

Client

Import COM.IN.SMTP.EMAILFORSMTP;


public class Client {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
EMAILFORSMTP EFS = new EMAILFORSMTP ();

Efs.sethost ("smtp.163.com");
Efs.setuserinformaition ("Manytao", "wangtao521");
Efs.sendemail ("

}

Ssss@163.com "," bbb_bbb@163.com "," AAAA "," bbbbb ");


}

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.