A simple example of sending emails in Java

Source: Internet
Author: User
Package com. jinfonet. KB;

Import java. util. properties;

Import javax. Mail. messagingexception;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. internetaddress;

Import com. Sun. Mail. SMTP. smtpmessage;

Public class mailsender {

Protected parametersmanager PM = parametersmanager. getinstance ();

Public void sendtouser (string email, string content ){
Properties mailprop = new properties ();
Try {
Mailprop
. Setproperty ("mail. SMTP. Host", PM. getitem ("mail. User. Host "));
Mailprop
. Setproperty ("mail. SMTP. Port", PM. getitem ("mail. User. Port "));

Session session = session. getdefaultinstance (mailprop );

Smtpmessage mail = new smtpmessage (session );
Internetaddress from = new internetaddress (PM
. Getitem ("mail. User. Sender "));
Mail. setfrom (from );

Internetaddress [] receiver = new internetaddress [1];
Receiver [0] = new internetaddress (email );
Mail. setrecipients (javax. Mail. Message. recipienttype. To, receiver );

Mail. setsubject (PM. getitem ("mail. User. Subject "));
Mail. setcontent (content, "text/html; charset = UTF-8 ");

Transport. Send (Mail );

} Catch (messagingexception me ){
System. Out. println (Me. tostring ());
Me. printstacktrace ();
} Catch (exception e ){
System. Out. println (E. tostring ());
E. printstacktrace ();
}
}

Public void sendtoproductowner (string content, string sender ){

Properties mailprop = new properties ();
Try {
Mailprop. setproperty ("mail. SMTP. Host", PM
. Getitem ("mail. Owner. Host "));
Mailprop. setproperty ("mail. SMTP. Port", PM
. Getitem ("mail. Owner. Port "));

Session session = session. getdefaultinstance (mailprop );

Smtpmessage mail = new smtpmessage (session );
Internetaddress from = NULL;
If (sender = NULL ){
From = new internetaddress (PM. getitem ("mail. Owner. Sender "));
} Else {
From = new internetaddress (sender );
}

String [] rece = PM. getitem ("mail. Owner. Cycler"). Split (",");
Internetaddress [] to = new internetaddress [rece. Length];
For (INT I = 0; I <rece. length; I ++ ){
To [I] = new internetaddress (string) rece [I]);
}

Mail. setfrom (from );

Mail. setrecipients (javax. Mail. Message. recipienttype. To, );

Mail. setsubject (PM. getitem ("mail. Owner. Subject "));
Mail. setcontent (content, "text/html; charset = UTF-8 ");

Transport. Send (Mail );
} Catch (messagingexception me ){
System. Out. println (Me. tostring ());
Me. printstacktrace ();
} Catch (exception e ){
System. Out. println (E. tostring ());
E. printstacktrace ();
}
}

Public static void main (string s []) {
Mailsender MS = new mailsender ();
Ms. sendtoproductowner ("hello", null );
MS = NULL;
}

}

Package com. jinfonet. KB;

Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. unsupportedencodingexception;
Import java.net. url;
Import java.net. urldecoder;
Import java. util. properties;

Public class parametersmanager {
Private Static string pfile = NULL;

Private file = NULL;

Private long lastmodifiedtime = 0;

Private Properties props = NULL;

Private Static parametersmanager instance = new parametersmanager ();

Private parametersmanager (){

URL url = This. getclass (). getresource ("config. properties ");

If (URL! = NULL ){
Try {
String Path = urldecoder. Decode (URL. getpath (), "UTF-8 ");
/*
* For Unix filesystem is file:/path for Windows filesystem is
* File:/X:/path or file:/D:/path
*/
If (path. Length ()> = 3) & (path. charat (0) = '/')
& (Path. charat (2) = ':')){
Path = path. substring (1 );
}
Pfile = path. Replace ('/', file. separatorchar );
} Catch (unsupportedencodingexception ex ){
System. Out. println (ex. tostring ());
}
}
File = new file (pfile );

Lastmodifiedtime = file. lastmodified ();
If (lastmodifiedtime = 0 ){
System. Out. println (pfile + "file does not exist! ");
}
Props = new properties ();
Try {
Props. Load (New fileinputstream (pfile ));
} Catch (filenotfoundexception ex ){
System. Out. println (ex. tostring ());
} Catch (ioexception e ){
System. Out. println (E. tostring ());
}
}

/* Return signal instance */
Synchronized public static parametersmanager getinstance (){
Return instance;
}

Public String getitem (string key ){
Long newtime = file. lastmodified ();
If (newtime> lastmodifiedtime ){
Props. Clear ();
Try {
Props. Load (New fileinputstream (pfile ));
} Catch (ioexception e ){
System. Out. println (E. tostring ());
}
}
Lastmodifiedtime = newtime;
Return props. getproperty (key );
}

Public void setitem (string key, string value ){
String header = "paramers of jinfonet MERs self-service center ";
Try {
Props. setproperty (Key, value );
Props. Store (New fileoutputstream (pfile), header );
} Catch (ioexception e ){
System. Out. println (E. tostring ());
}
}
}

Config. Properties

Mail. Owner. Port = 25
Mail. Owner. Sender = jreportcustomercareportal
Mail. Owner. Cycler = liyuan.li@china.jinfonet.com
Mail. Owner. Host = 192.168.128.4
Mail. Owner. Subject = jinfonet Technical Support

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.