Use the JavaMail API to write a mail-issuing program

Source: Internet
Author: User
Tags prepare
With the development of Internet and the popularization of computer application, e-mail is becoming an important way of communication. At present, there are a variety of development tools that can be used to prepare mail-sending programs, which are easiest to use JavaMail API programming. This article provides a complete example of how to use the JavaMail API to prepare mail-sending programs.
Readers who do not have the JavaMail API loaded can download JavaMail API 1.2 or JavaMail API 1.3 from the Www.javasoft.com site and set Readme.txt according to Classpath. The example code is as follows:
Import java.util.*;
Import java.io.*;
Import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;
public class Jsendmail {
Define recipients, senders, topics, etc.
String to= "", from= "", host= "", Filename= "", messagetext= "", subject= "";
Boolean debug=false;
Save the sent file name
Vector vfile = new vector (10,10);
Save the Sent body
Vector vmsg = new vector (10,10);
Public Jsendmail () {}
Public Jsendmail (String to,string from,string smtpserver,string subject)
{
Initialize recipients, senders, topics
This.to=to;
This.from=from;
This.host=smtpserver;
This.subject=subject;
}
Collect Additional Files
public void Attachfile (String fname)
{
Vfile.addelement (fname);
}
Collect message body
public void Setmessage (String msg)
{
Vmsg.addelement (msg);
}
Set Debug Flags
public void Setdebug (Boolean debug)
{
This.debug=debug;
}
Mail Send function
public boolean startsend ()
{
Properties props = System.getproperties ();
Props.put ("Mail.smtp.host", host);
Session Session=session.getdefaultinstance
(props, NULL);
Session.setdebug (Debug);
try {
Creates a message and initializes each element of the message
MimeMessage msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress (from));

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.