How to send an HTML message inside a Java program __html

Source: Internet
Author: User

Are catching up every year, and children ask me how to send emails in Java programs, especially in HTML format, put an example here:


MAVEN introduces JavaMail


        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>


and write a simple tool class.


Package com.xxx.tools;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;

Import javax.mail.internet.MimeUtility;

    public class Htmlemail {private String smtpserver;

    Private String username;

    private String password; public void SendMessage (string to, string subject, String messagetext) throws Messagingexception,
        java.io.UnsupportedEncodingException {java.util.Properties props = new Java.util.Properties (); Props.setproperty ("Mail.smtp.auth", "true");//Specify whether SMTP authentication Props.setproperty ("Mail.smtp.host", smtpserver) is required;//Specify SM
        TP Server Props.put ("Mail.transport.protocol", "SMTP");
        Session mailsession = session.getdefaultinstance (props);
        Mailsession.setdebug (TRUE)//Whether debug information is displayed on the console internetaddress fromaddress = new internetaddress (username); InternetAddress toaddress = new Internetaddress (to);
        MimeMessage emailmessage = new MimeMessage (mailsession);
        Emailmessage.setfrom (fromaddress);
        Emailmessage.addrecipient (Javax.mail.Message.RecipientType.TO, toaddress);
        Emailmessage.setsentdate (New Java.util.Date ());

        Emailmessage.setsubject (Mimeutility.encodetext (Subject, "Utf-8", "B"));

        
        Emailmessage.setcontent (MessageText, "text/html;charset=utf-8");
        Transport transport = Mailsession.gettransport ("SMTP");
        Transport.connect (smtpserver, username, password);
        Transport.sendmessage (Emailmessage, emailmessage.getallrecipients ());
    Transport.close ();
    Public String Getsmtpserver () {return smtpserver;
    } public void Setsmtpserver (String smtpserver) {this.smtpserver = SmtpServer;
    Public String GetUserName () {return username;
    } public void Setusername (String username) {this.username = username; } public String GETPASSWOrd () {return password;
    } public void SetPassword (String password) {this.password = password; }
}


And then configure it in spring


    <bean id= "Htmlmail" class= "Com.xxx.tools.HtmlEmail" >
        <property name= "smtpserver" value= "smtp.qq.com"/ >
        <property name= "username" value= "1xxxx7@qq.com" ></property>
        <property name= "Password" Value= "xxxxxxx" ></property>
    </bean>


Then use it where you want to use it:

@Controller
@RequestMapping ("/user") public
class Userregcontroller {

    @Resource
    private Htmlemail email;

    @RequestMapping ("/reg") public
    void SendMail (@RequestParam ("username") String username, @RequestParam ("Userpass ") string userpass, @RequestParam (" email ") string email_addr, HttpServletRequest request) {

        string valid_string = req Uest.getsession (True). GetId ();

        String Valid_url = "<a href= ' http://localhost:8080/user/valid?username=" + username + "&validstring=" + valid_ String + "' target= ' _blank ' > Click this link to activate account </a>";

        try {
            email.sendmessage (email_addr, "user activated", valid_url);
        } catch (Messagingexception e) {
            E.printstacktrace ();
        } catch (Unsupportedencodingexception e) {
            e.printstacktrace ();
        }

    }



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.