Java instance code for sending mail (portable) _java

Source: Internet
Author: User

Take a Web project as an example, the code is portable

First you import the Mail.jar package, and then you create your own class

1:htmlsender class

Copy Code code as follows:

Package com.txq.mail;

Import java.util.Properties;

Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.NoSuchProviderException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
Import javax.mail.internet.MimeUtility;

/**
*
* @author Administrator
*
*/
public class Htmlsender {
Private MimeMessage message;
Private Properties props;
Private session session;
Private String name = "";
Private String Password = "";

Public Htmlsender (String host,string name,string password)
{
THIS.name = name;
This.password = password;
Props = System.getproperties ();

Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Myauthenticator auth = new Myauthenticator (Name,password);
Session = Session.getdefaultinstance (Props,auth);

message = new MimeMessage (session);
New Mimemultipart ();
}

public void Setfrom (String from)
{
Try
{
Message.setfrom (New InternetAddress (from));
}
catch (Addressexception e)
{
E.printstacktrace ();
}
catch (Messagingexception e)
{
E.printstacktrace ();
}
}

public void Setto (String to)
{
Try
{
Message.setrecipients (To) (Message.recipienttype.to,internetaddress.parse);
}
catch (Addressexception e)
{
E.printstacktrace ();
}
catch (Messagingexception e)
{
E.printstacktrace ();
}
}

public void Setsubject (String subject)
{
Try
{
Message.setsubject (subject);
}
catch (Exception e)
{
E.printstacktrace ();
}
}

public void Setcontect (String content)
{
Try
{
Message.setcontent (Content, "text/html;charset=utf-8");
}
catch (Exception e)
{
E.printstacktrace ();
}
}

public boolean Send ()
{
Try
{
Transport transport = Session.gettransport ("SMTP");
Transport.connect ((String) props.get ("Mail.smtp.host"), Name,password);
Transport.sendmessage (Message, message.getrecipients (Message.RecipientType.TO));
Transport.close ();
return true;
}
catch (Nosuchproviderexception e)
{
E.printstacktrace ();
return false;
}
catch (Messagingexception e)
{
E.printstacktrace ();
return false;
}
}
}

2:myauthenticator class

Copy Code code as follows:

Package com.txq.mail;

Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;


public class Myauthenticator extends authenticator{
String name;
String password;
Public Myauthenticator (String name,string pasword)
{
THIS.name = name;
This.password = Pasword;
Getpasswordauthentication ();
}
Protected Passwordauthentication getpasswordauthentication ()
{
return new Passwordauthentication (Name,password);
}
}

3:mailaction class

Copy Code code as follows:

Package com.txq.action;

Import Java.io.PrintWriter;

Import Com.opensymphony.xwork2.ActionSupport;
Import Com.txq.mail.HTMLSender;

public class Mailaction extends Actionsupport {
Private String Email;
Public String SendEmail () {
Agent Host-Sina
String host = "smtp.sina.cn";
Account number-Sina account
String name = "Followmusic";
Password-Sina password
String password = "Followmusic";
Create a Send mail object
Htmlsender mail = new Htmlsender (Host,name,password);
Email account
Mail.setfrom ("followmusic@sina.cn");
e-Mail Billing Account
Mail.setto ("youxiangzhanghe@qq.com");
Message subject
Mail.setsubject ("Amoy interest");
Message content
Mail.setcontect ("Your friend invites you to Amoy interest ~");

if (Mail.send ())
{
return SUCCESS;
}
Else
{
return "error";
}
}
Public String Getemail () {
return email;
}
public void Setemail (String email) {
This.email = email;
}
}

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.