1. 程式人生 > >JavaMail 傳送郵件的程式

JavaMail 傳送郵件的程式

JavaMail01: 

package com.yuming.test;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMail01 {
	/**
	 * 單一
	 * @param args
	 * @throws AddressException
	 * @throws MessagingException
	 */
	public static void main(String[] args) throws Exception {
		
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		Session session = Session.getInstance(props);
		session.setDebug(true);
		
		Message msg = new MimeMessage(session);
		msg.setText("好好的開始都是");
		msg.setFrom(new InternetAddress("
[email protected]
")); Transport transport = session.getTransport(); transport.connect("smtp.sina.com", 25, "itcast_test", "123456"); transport.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")}); //transport.send(msg,new Address[]{new InternetAddress("[email protected]
")}); transport.close(); } }