Java Program for sending mail

Source: Internet
Author: User
Tags date mail tostring stringbuffer
program | Send an email the following is an example of HTML to send a message for everyone to write e-mail reference.
The function is to send the UL address of the HTML to the mail. The pictures will be sent to the user with the mail.

Package Com.fswan.memo;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.Properties;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

Import Javax.activation.DataHandler;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
/**
* @author Swan Fong (Fang Zhiwen)
*
*/
public class Testmail
{
public static final String ul = "http://www.qxzw.com/htmpage/0/14/123.htm";
public static void Main (string[] args)
{
Try
{
URL url = new URL (ul);
InputStream ins = Url.openstream ();
SendMail (URL, "Newsbot@yeah.net", "smtp.yeah.net");
catch (Malformedurlexception e)
{
E.printstacktrace ();
}
}
public static void SendMail (URL url,string mail,string smtpserver)
{
SYSTEM.OUT.PRINTLN ("send");
Mimemultipart MP = new Mimemultipart ();
String idstr = new SimpleDateFormat ("Hhmmsss"). Format (new Date ());
System.out.println (IDSTR);
Try
{
InputStream ins = Url.openstream ();
StringBuffer sb = new StringBuffer ();
byte[] conts = new byte[10240];
int len = Ins.read (conts);
while (Len > 0)
{
Sb.append (New String (Conts,0,len));
System.out.println (New String (conts));
Conts = new byte[10240];
Len = Ins.read (conts);
}
String con = sb.tostring ();
String Con2 = sb.tostring ();
SB = new StringBuffer ();
String regex = "]*) \"?. */?\\s*> ";
Pattern p = pattern.compile (regex);
Matcher m = P.matcher (con);
ArrayList _mailattachment = new ArrayList ();
int lastpos = 0;
while (M.find ())
{
Sb.append (Con.substring (Lastpos, M.start ()));
Sb.append (M.group (). ReplaceAll (M.group (1), "CID:" + Idstr + _mailattachment.size ()));
_mailattachment.add (new URL (URL, m.group (1));
Lastpos = M.end ();
}
Sb.append (Con.substring (Lastpos, Con.length ()));

MimeBodyPart MBP2 = new MimeBodyPart ();
Mbp2.setcontent (Sb.tostring (), "text/html; charset=gb2312");
Mp.addbodypart (MBP2);
for (int i = 0; i < _mailattachment.size (); i++)
{
MimeBodyPart MBP = new MimeBodyPart ();
Mbp.setdatahandler (New DataHandler ((URL) _mailattachment.get (i)));
Mbp.setheader ("Content-id", idstr+i);
Mp.addbodypart (MBP);
}

Properties prop = new properties ();
Prop.put ("Mail.smtp.host", smtpserver);
Prop.put ("Mail.smtp.auth", "true");
Session s = session.getinstance (prop, New authenticator () {
Public Passwordauthentication getpasswordauthentication ()
{
return new Passwordauthentication ("Newsbot@yeah.net", "fswan123");
}
});
MimeMessage message = new MimeMessage (s);
Message.setsubject ("Mail");
Message.setfrom (New internetaddress (mail));
Message.setrecipient (Message.RecipientType.TO, New internetaddress (mail));
Message.setcontent (MP);
Transport.send (message);
catch (Malformedurlexception e)
{
E.printstacktrace ();
catch (IOException E)
{
E.printstacktrace ();
catch (Messagingexception e)
{
E.printstacktrace ();
}
System.out.println ("finished");
}
}




Related Article

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.