本文整理匯總了Java中javax.mail.internet.MimeMessage.setFrom方法的典型用法代碼示例。如果您正苦於以下問題:Java MimeMessage.setFrom方法的具體用法?Java MimeMessage.setFrom怎麽用?Java MimeMessage.setFrom使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.mail.internet.MimeMessage
的用法示例。
在下文中一共展示了MimeMessage.setFrom方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createCodeMessage
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
/**
* 創建一封郵件
*
* @param session 和服務器交互的會話
* @param sendMail 發件人郵箱
* @param receiveMail 收件人郵箱
* @return
* @throws Exception
*/
private MimeMessage createCodeMessage(Session session, String sendMail, String receiveMail) throws MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sendMail, "吃在華科", "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, this.name, "UTF-8"));
message.setSubject("吃在華科郵件注冊驗證碼", "UTF-8");
String content = this.name + ",你好, 您的驗證碼如下<br/>" + getCode() + "<p> 您不需要回複這封郵件。<p/>";
message.setContent(content, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message;
}
示例2: sendMailBatch
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public void sendMailBatch(List<String> to, String title, String content) throws MessagingException {
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
to.forEach(address -> {
try {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
} catch (MessagingException ignored) {
}
});
message.setSubject(title);
message.setContent(content, "text/html;charset=UTF-8");
Transport.send(message);
}
示例3: sendTokenMail
import javax.mail.internet.MimeMessage; //導入方法依賴的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;
}
}
示例4: send
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
/**
* 發送郵件
* @param receiver 接收者郵箱地址
* @param subject 郵件主題
* @param content 郵件內容
*/
public static void send(String receiver, String subject, String content) {
try {
//設置SSL連接、郵件環境
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", SMTP_HOST);
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", SMTP_PORT);
props.setProperty("mail.smtp.socketFactory.port", SMTP_SOCKET_FACTORY_PORT);
props.setProperty("mail.smtp.auth", "true");
//建立郵件會話
Session session = Session.getDefaultInstance(props, new Authenticator() {
//身份認證
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SENDER, PASSWORD);
}
});
//建立郵件對象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(SENDER, SENDER_NAME));
message.setRecipients(Message.RecipientType.TO, receiver);
message.setSubject(subject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(content, "text/html;charset=UTF-8");
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(mimeBodyPart);
message.setContent(mimeMultipart);
message.saveChanges();
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
示例5: createEmailMessage
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
/**
* Bereitet den Mailinhalt zum Senden vor.
*
* @throws MessagingException -
* @throws UnsupportedEncodingException -
*/
public void createEmailMessage() throws MessagingException, UnsupportedEncodingException {
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
for (String toEmail : toEmailList) {
emailMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
}
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");
}
示例6: createEmail
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public static MimeMessage createEmail(String to, String from, String subject,
String bodyText) throws MessagingException{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress fAddress = new InternetAddress(from);
email.setFrom(fAddress);
email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
email.setSubject(subject);
email.setText(bodyText);
return email;
}
示例7: sendEmail
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public static void sendEmail(EmailSettingContent emailSetting, String acceptor, String subject, String body) {
Properties props = buildProperty(emailSetting);
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = null;
String password = null;
if (emailSetting.isAuthenticated()) {
username = emailSetting.getUsername();
password = emailSetting.getPassword();
}
return new PasswordAuthentication(username, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailSetting.getSender()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(acceptor));
message.setSubject(subject, "utf8");
message.setContent(body, "text/html;charset=utf8");
Transport.send(message);
} catch (Throwable throwable) {
}
}
示例8: test
import javax.mail.internet.MimeMessage; //導入方法依賴的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();
}
}
示例9: enviarMail
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public static void enviarMail(Persona cliente, String asunto, String mensaje) {
if (cliente.getPersonaMail() != null) {
Mail mail = obtenerMailEmisor();
Properties props = conectarse(mail);
Session session = autentificar(mail, props);
try {
BodyPart texto = new MimeBodyPart();
// Texto del mensaje
texto.setText(mensaje);
MimeMultipart multiParte = new MimeMultipart();
multiParte.addBodyPart(texto);
MimeMessage message = new MimeMessage(session);
// Se rellena el From
InternetAddress emisor = new InternetAddress(mail.getNombre() + " <" + mail.getMail() + ">");
message.setFrom(emisor);
// Se rellenan los destinatarios
InternetAddress receptor = new InternetAddress();
receptor.setAddress(cliente.getPersonaMail());
message.addRecipient(Message.RecipientType.TO, receptor);
// Se rellena el subject
message.setSubject(asunto);
// Se mete el texto y la foto adjunta.
message.setContent(multiParte);
Transport.send(message);
} catch (MessagingException e) {
messageService.informUser("Poliza creada, falló envío de mail");
}
}
}
示例10: sendMultipartMessage
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public void sendMultipartMessage(String subject, String[] to, String text, String attach)
throws MessagingException, IOException {
MimeMessage message = new MimeMessage(senderSession);
message.setFrom(new InternetAddress(pManager.get_SENDER_From())); // FROM
for(int i=0; i < to.length; i++) {
if(!to[i].equals("")) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); // TO
}
}
message.setSubject(subject); //SUBJECT
Multipart mp = new MimeMultipart();
BodyPart textPart = new MimeBodyPart();
textPart.setText(text);
mp.addBodyPart(textPart); // TEXT
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(attach);
mp.addBodyPart(attachPart); // ATTACH
message.setContent(mp);
transport.sendMessage(message, message.getAllRecipients());
}
示例11: createAttachMail
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
/**
* @Method: createAttachMail
* @Description: ����һ����������ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(String subject, String content, String filePath) throws Exception{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(filePath));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
return message;
}
示例12: send
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public boolean send() throws Exception {
Properties props = _setProperties();
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[] {
new InternetAddress("[email protected]")
});
msg.setSentDate(new Date());
msg.setContent(multipart);
Transport.send(msg);
return true;
}
示例13: sendMail
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public void sendMail(String account) throws Exception {
message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(account, false));
message.setSubject("DO NOT REPLY");
message.setDataHandler(new DataHandler(new ByteArrayDataSource(getMailContent(), "text/html")));
message.setSentDate(new Date());
transport = session.getTransport();
transport.connect("smtp-mail.outlook.com", "[email protected]", "BaoZJ960606");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例14: send
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
public void send(String to, String cc, String bcc, String subject,
String content, JavamailConfig javamailConfig)
throws MessagingException {
logger.debug("send : {}, {}", to, subject);
try {
Properties props = createSmtpProperties(javamailConfig);
String username = javamailConfig.getUsername();
String password = javamailConfig.getPassword();
// 創建Session實例對象
Session session = Session.getInstance(props, new SmtpAuthenticator(
username, password));
session.setDebug(false);
// 創建MimeMessage實例對象
MimeMessage message = new MimeMessage(session);
// 設置郵件主題
message.setSubject(subject);
// 設置發送人
message.setFrom(new InternetAddress(username));
// 設置發送時間
message.setSentDate(new Date());
// 設置收件人
message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
// 設置html內容為郵件正文,指定MIME類型為text/html類型,並指定字符編碼為gbk
message.setContent(content, "text/html;charset=gbk");
// 保存並生成最終的郵件內容
message.saveChanges();
// 發送郵件
Transport.send(message);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
示例15: Send
import javax.mail.internet.MimeMessage; //導入方法依賴的package包/類
/**
* Send email using GMail SMTP server.
*
* @param username GMail username
* @param password GMail password
* @param recipientEmail TO recipient
* @param ccEmail CC recipient. Can be empty if there is no CC recipient
* @param title title of the message
* @param message message to be sent
* @throws AddressException if the email address parse failed
* @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
*/
private static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException, EmailInesistente {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtps.auth", "true");
/*
If set to false, the QUIT command is sent and the connection is immediately closed. If set
to true (the default), causes the transport to wait for the response to the QUIT command.
ref : http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
http://forum.java.sun.com/thread.jspa?threadID=5205249
smtpsend.java - demo program from javamail
*/
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username + "@gmail.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("smtp.gmail.com", username, password);
try{
t.sendMessage(msg, msg.getAllRecipients());
}
catch (SendFailedException e){
throw new EmailInesistente();
}
t.close();
}