Java操作應(yīng)用——郵件發(fā)送
在Java中發(fā)送郵件也很簡單。你只需裝一下Java Mail這個jar包,放到你的類路徑里即可。在下面的代碼中,我們設(shè)置了幾個基礎(chǔ)屬性,然后便可以發(fā)送郵件了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String [] args) { String to = "recipient@gmail.com" ; String from = "sender@gmail.com" ; String host = "localhost" ; Properties properties = System.getProperties(); properties.setProperty( "mail.smtp.host" , host); Session session = Session.getDefaultInstance(properties); try { MimeMessage message = new MimeMessage(session); message.setFrom( new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject( "My Email Subject" ); message.setText( "My Message Body" ); Transport.send(message); System.out.println( "Sent successfully!" ); } catch (MessagingException ex) { ex.printStackTrace(); } } } |
點擊加載更多評論>>