當前位置: 首頁>>代碼示例>>Java>>正文


Java Transport.sendMessage方法代碼示例

本文整理匯總了Java中javax.mail.Transport.sendMessage方法的典型用法代碼示例。如果您正苦於以下問題:Java Transport.sendMessage方法的具體用法?Java Transport.sendMessage怎麽用?Java Transport.sendMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.mail.Transport的用法示例。


在下文中一共展示了Transport.sendMessage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: completeClientSend

import javax.mail.Transport; //導入方法依賴的package包/類
public void completeClientSend(String mailServer, String... credentials) throws AddressException, MessagingException {
	if (credentials != null && credentials.length > 1) {
		// Step1
		logger.info("\n 1st ===> setup Mail Server Properties..");
		logger.info("Mail Server Properties have been setup successfully..");
		// Step2
		logger.info("\n\n 2nd ===> get Mail .");
		getMailSession = getDefaultInstance(mailServerProperties, null);
		generateMailMessage = new MimeMessage(getMailSession);
		generateMailMessage.addRecipient(TO, new InternetAddress("[email protected]"));
		generateMailMessage.addRecipient(CC, new InternetAddress("[email protected]"));
		generateMailMessage.setSubject("Greetings from Vige..");
		String emailBody = "Test email by Vige.it JavaMail API example. " + "<br><br> Regards, <br>Vige Admin";
		generateMailMessage.setContent(emailBody, "text/html");
		logger.info("Mail Session has been created successfully..");
		// Step3
		logger.info("\n\n 3rd ===> Get Session and Send mail");
		Transport transport = getMailSession.getTransport("smtp");
		// Enter your correct gmail UserID and Password
		// if you have 2FA enabled then provide App Specific Password
		transport.connect(mailServer, credentials[0], credentials[1]);
		transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
		transport.close();
	}
}
 
開發者ID:PacktPublishing,項目名稱:Mastering-Java-EE-Development-with-WildFly,代碼行數:26,代碼來源:SendMail.java

示例2: sendToAdmin

import javax.mail.Transport; //導入方法依賴的package包/類
@Override
public boolean sendToAdmin(final String institutionEmailAddress, final String applicationMessage) {
    String[] to = new String[]{"[email protected]"};
    setupProperties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(username));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for (int i = 0; i < to.length; i++) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for (int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject(APPLICATION_SUBJECT);
        message.setText(applicationBody + "Email: " + institutionEmailAddress + "\nMessage: " + applicationMessage);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        LOGGER.info("Email sent to Admin");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:blmalone,項目名稱:Blockchain-Academic-Verification-Service,代碼行數:32,代碼來源:EmailService.java

示例3: sendTokenMail

import javax.mail.Transport; //導入方法依賴的package包/類
@Override
public boolean sendTokenMail(final String[] to, final String contractAddress, final String artifactId,
                             final String registryContract) {
    setupProperties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(username));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for (int i = 0; i < to.length; i++) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for (int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject(TOKEN_SUBJECT);
        message.setText(tokenBody + contractAddress + "\nArtifact ID: " + artifactId
                + "\n\nBelow is the Unilog Registry. Add this into the verifiers portal if you need to"
                + " verify a token: \n" + registryContract);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        LOGGER.info("Token email has been sent");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:blmalone,項目名稱:Blockchain-Academic-Verification-Service,代碼行數:34,代碼來源:EmailService.java

示例4: send

import javax.mail.Transport; //導入方法依賴的package包/類
/**
 * @Title: send
 * @Description: TODO(發送帶附件的郵件)
 * @author [email protected] (苟誌強)
 * @date  2017-6-6 下午4:29:56
 * @param to 接收用戶賬戶
 * @param title 郵件標題
 * @param content 郵件內容
 * @param affix 附件路徑
 * @param affixName 發送後顯示名稱
 * @return 是否發送成功
 */
public static boolean send(String to,String title,String content,String affix,String affixName) {
	Properties props = new Properties();
	//設置發送郵件的郵件服務器的屬性(這裏使用網易的smtp服務器)
	props.put("mail.smtp.host", host);
	//需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證
	props.put("mail.smtp.auth", "true");
	//用剛剛設置好的props對象構建一個session
	Session session = Session.getDefaultInstance(props);
	//有了這句便可以在發送郵件的過程中在console處顯示過程信息,供調試使
	//用(你可以在控製台(console)上看到發送郵件的過程)
	session.setDebug(true);
	//用session為參數定義消息對象
	MimeMessage message = new MimeMessage(session);
	try{
		//加載發件人地址
		message.setFrom(new InternetAddress(from));
		//加載收件人地址
		message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
		//加載標題
		message.setSubject(title);
		// 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
		Multipart multipart = new MimeMultipart();         

		//設置郵件的文本內容
		BodyPart contentPart = new MimeBodyPart();
		contentPart.setText(content);
		multipart.addBodyPart(contentPart);
		//添加附件
		if(affix!=null&&!"".equals(affix)&&affixName!=null&&!"".equals(affixName)){
			BodyPart messageBodyPart= new MimeBodyPart();
			DataSource source = new FileDataSource(affix);
			//添加附件的內容
			messageBodyPart.setDataHandler(new DataHandler(source));
			//添加附件的標題
			messageBodyPart.setFileName(MimeUtility.encodeText(affixName));
			multipart.addBodyPart(messageBodyPart);
		}
		//將multipart對象放到message中
		message.setContent(multipart);
		//保存郵件
		message.saveChanges();
		//   發送郵件
		Transport transport = session.getTransport("smtp");
		//連接服務器的郵箱
		transport.connect(host, user, pwd);
		//把郵件發送出去
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
		return true;
	}catch(Exception e){
		e.printStackTrace();
		return false;
	}
}
 
開發者ID:zhiqiang94,項目名稱:BasicsProject,代碼行數:67,代碼來源:EmailUtil.java

示例5: sendFeedBackMail

import javax.mail.Transport; //導入方法依賴的package包/類
/**
 * 發送反饋郵件
 * @param detail
 * @throws UnsupportedEncodingException
 * @throws MessagingException
 */
public void sendFeedBackMail(String detail) throws UnsupportedEncodingException, MessagingException {
    MimeMessage message = createFeedBackMsg(session,myEmailAccount,detail);
    Transport transport = session.getTransport();
    transport.connect(myEmailAccount, myEmailPassword);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
}
 
開發者ID:GrayXu,項目名稱:HustEating,代碼行數:14,代碼來源:Mail.java

示例6: sendout

import javax.mail.Transport; //導入方法依賴的package包/類
/**
 * 發送郵件
 */
public boolean sendout() {
	try {
		mimeMsg.setContent(mp);
		mimeMsg.saveChanges();

		logger.info(Resources.getMessage("EMAIL.SENDING"));
		Session mailSession = Session.getInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				if (userkey == null || "".equals(userkey.trim())) {
					return null;
				}
				return new PasswordAuthentication(username, userkey);
			}
		});
		Transport transport = mailSession.getTransport("smtp");
		transport.connect((String) props.get("mail.smtp.host"), username, password);
		// 設置發送日期
		mimeMsg.setSentDate(new Date());
		// 發送
		transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
		if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) {
			transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
		}
		logger.info(Resources.getMessage("EMAIL.SEND_SUCC"));
		transport.close();
		return true;
	} catch (Exception e) {
		logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e);
		return false;
	}
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:35,代碼來源:EmailSender.java

示例7: sendout

import javax.mail.Transport; //導入方法依賴的package包/類
/**
 * 發送郵件
 * 
 * @param name String
 * @param pass String
 */
public boolean sendout() {
	try {
		mimeMsg.setContent(mp);
		mimeMsg.saveChanges();

		logger.info(Resources.getMessage("EMAIL.SENDING"));
		Session mailSession = Session.getInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				if (userkey == null || "".equals(userkey.trim())) {
					return null;
				}
				return new PasswordAuthentication(username, userkey);
			}
		});
		Transport transport = mailSession.getTransport("smtp");
		transport.connect((String) props.get("mail.smtp.host"), username, password);
		// 設置發送日期
		mimeMsg.setSentDate(new Date());
		// 發送
		transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
		if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) {
			transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
		}
		logger.info(Resources.getMessage("EMAIL.SEND_SUCC"));
		transport.close();
		return true;
	} catch (Exception e) {
		logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e);
		return false;
	}
}
 
開發者ID:guokezheng,項目名稱:automat,代碼行數:38,代碼來源:EmailSender.java

示例8: sendSetupMail

import javax.mail.Transport; //導入方法依賴的package包/類
@Override
public boolean sendSetupMail(final String[] to, final String code, final String subject, final String body) {
    setupProperties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(username));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for (int i = 0; i < to.length; i++) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for (int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        if (subject == null || body == null) {
            message.setSubject(SETUP_SUBJECT);
            message.setText(setupBody + code);
        } else {
            message.setSubject(subject);
            message.setText(body + code);
        }

        Transport transport = session.getTransport("smtp");
        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        LOGGER.info("Setup email has been sent.");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:blmalone,項目名稱:Blockchain-Academic-Verification-Service,代碼行數:37,代碼來源:EmailService.java

示例9: sendAlertEmail

import javax.mail.Transport; //導入方法依賴的package包/類
public void sendAlertEmail(String alert) throws MessagingException{
	String host = "smtp.gmail.com";
	String from = "[email protected]";
	String pass = "3inst3in?";
	Properties props = System.getProperties();
	props.put("mail.smtp.starttls.enable", "true"); // added this line
	props.put("mail.smtp.host", host);
	props.put("mail.smtp.user", from);
	props.put("mail.smtp.password", pass);
	props.put("mail.smtp.port", "587");
	props.put("mail.smtp.auth", "true");

	Session session = Session.getDefaultInstance(props, null);
	MimeMessage message = new MimeMessage(session);
	message.setFrom(new InternetAddress(from));

	InternetAddress[] toAddress = new InternetAddress[alert_recipients.length];

	// To get the array of addresses
	for( int i=0; i < alert_recipients.length; i++ ) { // changed from a while loop
		toAddress[i] = new InternetAddress(alert_recipients[i]);
	}
	//System.out.println(Message.RecipientType.TO);

	for( int i=0; i < toAddress.length; i++) { // changed from a while loop
		message.addRecipient(Message.RecipientType.TO, toAddress[i]);
	}
	message.setSubject("WARNING Something went wrong in the Einstein tunnel");
	message.setText(alert);
	Transport transport = session.getTransport("smtp");
	transport.connect(host, from, pass);
	transport.sendMessage(message, message.getAllRecipients());
	transport.close();

}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:36,代碼來源:Resetter.java

示例10: test

import javax.mail.Transport; //導入方法依賴的package包/類
public void test() throws Exception {

        Properties props = System.getProperties();
        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtp.port", PORT);

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");

        Session session = Session.getInstance(props);

        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setContent(BODY, "text/plain");

        Transport transport = session.getTransport();

        try {
            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
            transport.sendMessage(msg, msg.getAllRecipients());
        } finally {
            transport.close();
        }

    }
 
開發者ID:bamartinezd,項目名稱:traccar-service,代碼行數:29,代碼來源:NotificiationMailTest.java

示例11: sendAccountCreatedMail

import javax.mail.Transport; //導入方法依賴的package包/類
@Override
public boolean sendAccountCreatedMail(final String[] to, final String email) {
    setupProperties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(username));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for (int i = 0; i < to.length; i++) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for (int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject(DETAILS_SUBJECT);
        message.setText(detailsBody + "username: " + email);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        LOGGER.info("Login details email has been sent.");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:blmalone,項目名稱:Blockchain-Academic-Verification-Service,代碼行數:31,代碼來源:EmailService.java

示例12: send

import javax.mail.Transport; //導入方法依賴的package包/類
/**
 * 發送郵件
 * 
 * @param email-收件人
 * @throws Exception
 * @return new password
 */
public static String send(String email, String username) throws Exception {

	String pwd = getRandomString(6);
	
	// 2. 根據配置創建會話對象, 用於和郵件服務器交互
	Session session = Session.getDefaultInstance(props);
	session.setDebug(true); // 設置為debug模式, 可以查看詳細的發送 log

	// 3. 創建一封郵件
	MimeMessage message = createMimeMessage(session, myEmailAccount, email, username,pwd);

	// 4. 根據 Session 獲取郵件傳輸對象
	Transport transport = session.getTransport();

	// 5. 使用 郵箱賬號 和 密碼 連接郵件服務器, 這裏認證的郵箱必須與 message 中的發件人郵箱一致, 否則報錯
	//
	// PS_01: 成敗的判斷關鍵在此一句, 如果連接服務器失敗, 都會在控製台輸出相應失敗原因的 log,
	// 仔細查看失敗原因, 有些郵箱服務器會返回錯誤碼或查看錯誤類型的鏈接, 根據給出的錯誤
	// 類型到對應郵件服務器的幫助網站上查看具體失敗原因。
	//
	// PS_02: 連接失敗的原因通常為以下幾點, 仔細檢查代碼:
	// (1) 郵箱沒有開啟 SMTP 服務;
	// (2) 郵箱密碼錯誤, 例如某些郵箱開啟了獨立密碼;
	// (3) 郵箱服務器要求必須要使用 SSL 安全連接;
	// (4) 請求過於頻繁或其他原因, 被郵件服務器拒絕服務;
	// (5) 如果以上幾點都確定無誤, 到郵件服務器網站查找幫助。
	//
	// PS_03: 仔細看log, 認真看log, 看懂log, 錯誤原因都在log已說明。
	transport.connect(myEmailAccount, myEmailPassword);

	// 6. 發送郵件, 發到所有的收件地址, message.getAllRecipients() 獲取到的是在創建郵件對象時添加的所有收件人,
	// 抄送人, 密送人
	transport.sendMessage(message, message.getAllRecipients());

	// 7. 關閉連接
	transport.close();
	
	return pwd;
}
 
開發者ID:miracle857,項目名稱:weibo,代碼行數:47,代碼來源:Mail.java

示例13: sendEmail

import javax.mail.Transport; //導入方法依賴的package包/類
/**
 * Versendet die EMail.
 *
 * @throws MessagingException -
 */
public void sendEmail() throws MessagingException {
    Transport transport = mailSession.getTransport("smtp");
    String    emailHost = "smtp.gmail.com";
    transport.connect(emailHost, fromEmail, fromPassword);
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
}
 
開發者ID:LCA311,項目名稱:leoapp-sources,代碼行數:13,代碼來源:MailClient.java

示例14: send

import javax.mail.Transport; //導入方法依賴的package包/類
public void send ( final String subject, final String text ) throws MessagingException
{
    final Message message = new MimeMessage ( this.session );

    if ( this.from != null )
    {
        message.setFrom ( this.from );
    }
    else
    {
        message.setFrom ();
    }

    message.setHeader ( "Return-Path", "<>" );

    message.setRecipients ( javax.mail.Message.RecipientType.TO, this.to );
    message.setSubject ( subject );
    message.setText ( text );

    message.saveChanges ();

    final Transport transport = this.session.getTransport ();
    logger.debug ( "Connecting transport..." );
    transport.connect ();
    logger.debug ( "Connecting transport... done!" );
    try
    {
        logger.debug ( "Sending message..." );
        transport.sendMessage ( message, message.getAllRecipients () );
        logger.debug ( "Sending message... done!" );
    }
    finally
    {
        logger.debug ( "Closing transport..." );
        try
        {
            transport.close ();
            logger.debug ( "Closing transport... done!" );
        }
        catch ( final Exception e )
        {
            logger.info ( "Failed to close transport", e );
        }
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:46,代碼來源:MailSender.java

示例15: sendMail

import javax.mail.Transport; //導入方法依賴的package包/類
public boolean sendMail(String to,String otp) throws MessagingException 
{
	String host="smtp.gmail.com";
	String username="";//emailid
	String password="";//emailid password
	String from=" ";// email from which u have to send
	String subject="One Time Password";
	String body="Your One Time passsword is "+otp;
	
	boolean sessionDebug=false;
	
	Properties props=System.getProperties();
	props.put("mail.host",host);
	props.put("mail.transport.protocol","smtp");
	props.put("mail.smtp.starttls.enable","true");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.debug", "true");
	props.put("mail.smtp.socketFactory.port", "465");
	props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.socketFactory.fallback", "false");
	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.smtp.port", "25"); 
	
	Session mailSession=Session.getDefaultInstance(props,null);
	mailSession.setDebug(sessionDebug);
	
	Message msg=new MimeMessage(mailSession);
	msg.setFrom(new InternetAddress(from));
	InternetAddress [] address={new InternetAddress(to)};
	msg.setRecipients(Message.RecipientType.TO,address);
	msg.setSubject(subject);
	msg.setSentDate(new Date());
	msg.setText(body);

	Transport tr=mailSession.getTransport("smtp");
	tr.connect(host,username,password);
	msg.saveChanges();
	tr.sendMessage(msg,msg.getAllRecipients());
	tr.close();
	//Transport.send(msg);
	return true;
}
 
開發者ID:nishittated,項目名稱:OnlineElectionVotingSystem,代碼行數:43,代碼來源:PasswordMail.java


注:本文中的javax.mail.Transport.sendMessage方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。