javax.mail.Message.setRecipient()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.7k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中javax.mail.Message.setRecipient()方法的一些代码示例,展示了Message.setRecipient()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.setRecipient()方法的具体详情如下:
包路径:javax.mail.Message
类名称:Message
方法名:setRecipient

Message.setRecipient介绍

[英]Set the recipient address. All addresses of the specified type are replaced by the address parameter.

The default implementation uses the setRecipients method.
[中]设置收件人地址。指定类型的所有地址都将替换为address参数。
默认实现使用setRecipients方法。

代码示例

代码示例来源:origin: stackoverflow.com

import javax.mail.*;
import javax.mail.internet.*;

// Set up the SMTP server.
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "smtp.myisp.com");
Session session = Session.getDefaultInstance(props, null);

// Construct the message
String to = "you@you.com";
String from = "me@me.com";
String subject = "Hello";
Message msg = new MimeMessage(session);
try {
  msg.setFrom(new InternetAddress(from));
  msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
  msg.setSubject(subject);
  msg.setText("Hi,\n\nHow are you?");

  // Send the message.
  Transport.send(msg);
} catch (MessagingException e) {
  // Error.
}

代码示例来源:origin: aws/aws-sdk-java

m.setRecipient(Message.RecipientType.TO, addressTable.keySet().iterator().next());

代码示例来源:origin: com.sun.mail/javax.mail

/**
 * Computes the default to-address if none was specified.  This can
 * fail if the local address can't be computed.
 * @param msg the message
 * @param type the recipient type.
 * @since JavaMail 1.5.0
 */
private void setDefaultRecipient(final Message msg,
    final Message.RecipientType type) {
  try {
    Address a = InternetAddress.getLocalAddress(getSession(msg));
    if (a != null) {
      msg.setRecipient(type, a);
    } else {
      final MimeMessage m = new MimeMessage(getSession(msg));
      m.setFrom(); //Should throw an exception with a cause.
      Address[] from = m.getFrom();
      if (from.length > 0) {
        msg.setRecipients(type, from);
      } else {
        throw new MessagingException("No local address.");
      }
    }
  } catch (MessagingException | RuntimeException ME) {
    reportError("Unable to compute a default recipient.",
        ME, ErrorManager.FORMAT_FAILURE);
  }
}

代码示例来源:origin: aa112901/remusic

mailMessage.setFrom(from);
Address to = new InternetAddress("remusic_log@163.com");
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(title);
mailMessage.setSentDate(new Date());

代码示例来源:origin: camunda/camunda-bpm-platform

Address a = InternetAddress.getLocalAddress(getSession(msg));
if (a != null) {
  msg.setRecipient(type, a);
} else {
  final MimeMessage m = new MimeMessage(getSession(msg));

代码示例来源:origin: stackoverflow.com

public static void sendMail(String toAddress, String fromAddress, String subject, String body) throws AddressException, MessagingException {
  String smtpServer = getMXRecordsForEmailAddress(toAddress);

  // create session
  Properties props = new Properties();
  props.put("mail.smtp.host", smtpServer);
  Session session = Session.getDefaultInstance(props);

  // create message
  Message msg = new MimeMessage(session);
  msg.setFrom(new InternetAddress(fromAddress));
  msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
  msg.setSubject(subject);
  msg.setText(body);

  // send message
  Transport.send(msg);
}

代码示例来源:origin: tomoya92/pybbs

message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));

代码示例来源:origin: google/mail-importer

@Override
public void setRecipient(RecipientType type, Address address)
  throws RuntimeMessagingException {
 try {
  delegate.setRecipient(type, address);
 } catch (MessagingException e) {
  throw new RuntimeMessagingException(e);
 }
}

代码示例来源:origin: dkpro/dkpro-jwpl

protected Message initMessage() throws MessagingException {
  Message msg = new MimeMessage(MAIL_SESSION);
  msg.setFrom(new InternetAddress(ADDRESS_FROM));
  msg.setRecipient(Message.RecipientType.TO, new InternetAddress(ADDRESS_TO));
  msg.setSubject(subject);
  return msg;
}

代码示例来源:origin: de.tudarmstadt.ukp.wikipedia/de.tudarmstadt.ukp.wikipedia.wikimachine

protected Message initMessage() throws MessagingException {
  Message msg = new MimeMessage(MAIL_SESSION);
  msg.setFrom(new InternetAddress(ADDRESS_FROM));
  msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
      ADDRESS_TO));
  msg.setSubject(subject);
  return msg;
}

代码示例来源:origin: xpmatteo/birthday-greetings-kata

private void sendMessage(String smtpHost, int smtpPort, String sender, String subject, String body, String recipient) throws AddressException, MessagingException {
    // Create a mail session
    java.util.Properties props = new java.util.Properties();
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.port", "" + smtpPort);
    Session session = Session.getInstance(props, null);

    // Construct the message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(sender));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
    msg.setSubject(subject);
    msg.setText(body);

    // Send the message
    Transport.send(msg);
  }
}

代码示例来源:origin: TGAC/miso-lims

/**
  * Send an email to a recipient
  * 
  * @param to
  *          of type String
  * @param from
  *          of type String
  * @param subject
  *          of type String
  * @param text
  *          of type String
  * @param mailProps
  *          of type Properties
  * @throws javax.mail.MessagingException
  */
 public static void send(String to, String from, String subject, String text, Properties mailProps) throws MessagingException {
  Session mailSession = Session.getDefaultInstance(mailProps);
  Message simpleMessage = new MimeMessage(mailSession);

  InternetAddress fromAddress = new InternetAddress(from);
  InternetAddress toAddress = new InternetAddress(to);

  simpleMessage.setFrom(fromAddress);
  simpleMessage.setRecipient(Message.RecipientType.TO, toAddress);
  simpleMessage.setSubject(subject);
  simpleMessage.setText(text);

  Transport.send(simpleMessage);
 }
}

代码示例来源:origin: se.vgregion.mobile/mobile-mobile-composite-svc

@Override
public void report(ErrorReport report) {
  Properties props = new Properties();
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", port);
  
  Session mailSession = Session.getDefaultInstance(props);
  Message simpleMessage = new MimeMessage(mailSession);
  
  try {
    InternetAddress fromAddress = new InternetAddress(from);
    InternetAddress toAddress = new InternetAddress(to);
    simpleMessage.setFrom(fromAddress);
    simpleMessage.setRecipient(RecipientType.TO, toAddress);
    simpleMessage.setSubject(subject);
    
    String text = String.format(body, report.getPrinter().getName(), report.getDescription(), report.getReporter());
    simpleMessage.setText(text);
    
    Transport.send(simpleMessage);                  
  } catch (MessagingException e) {
    throw new RuntimeException("Failed sending error report via mail", e);
  }
  
}

代码示例来源:origin: stackoverflow.com

import java.util.Properties;

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

public class Mail
{
  public static void main(String[] args) throws MessagingException
  {
    Properties props = new Properties();
    props.setProperty("mail.smtp.host", "smtp.example.com");
    // props.setProperty("mail.smtp.auth", "true"); // not necessary for my server, I'm not sure if you'll need it
    Session session = Session.getInstance(props, null);
    Transport transport = session.getTransport("smtp");
    transport.connect("user", "password");

    Message message = new MimeMessage(session);
    message.setSubject("Test");
    message.setText("Hello :)");
    message.setFrom(new InternetAddress("you@example.com"));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("your-friend@example.com"));
    transport.sendMessage(message, message.getAllRecipients());
  }
}

代码示例来源:origin: org.nakedobjects/expenses-email

public void sendTextEmail(final String toEmailAddress, final String text) {
    final InternetAddress fromAddress;
    try {
      fromAddress = new InternetAddress(SMTP_AUTH_USER);
    } catch (AddressException e) {
      throw new ApplicationException("Invalid email address " + SMTP_AUTH_USER, e);
    }
    
    try {
      final Properties properties = new Properties();
      properties.put("mail.smtp.host", SMTP_HOST_NAME);
      properties.put("mail.smtp.auth", authenticate ? "true" : "false");
      final Authenticator authenticator = authenticate ? new SMTPAuthenticator() : null;
      final Session session = Session.getDefaultInstance(properties, authenticator);
      final Message message = new MimeMessage(session);
      final InternetAddress toAddress = new InternetAddress(toEmailAddress);
      message.setFrom(fromAddress);
      message.setRecipient(Message.RecipientType.TO, toAddress);
      message.setSubject("Expenses notification");
      message.setContent(text, "text/plain");
      Transport.send(message);
    } catch (AddressException e) {
      throw new ApplicationException("Invalid email address " + toEmailAddress, e);
    } catch (MessagingException e) {
      throw new ApplicationException("Problem sending email ", e);
    }
  }
}

代码示例来源:origin: stackoverflow.com

Message msg = new MimeMessage(mailSession);
 try {
   msg.setSubject(subject);
   msg.setText(body);
   msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
   msg.setFrom();
   Transport t = session.getTransport();
   if (!t.isConnected()) {
     t.connect();
   }
   t.sendMessage(msg, null);
 } catch (MessagingException ex) {
   // Handle exception
 } catch (UnsupportedEncodingException ex) {
   // Handle exception         
 }

代码示例来源:origin: org.mil-oss/fgsms-sla-processor

boolean sendTest(String email) {
  log.log(Level.INFO, "fgsms SLA Pusher test message");
  try {
    Properties props = SLACommon.LoadSLAPropertiesPooled();
    Session mailSession = Session.getDefaultInstance(props);
    Message simpleMessage = new MimeMessage(mailSession);
    InternetAddress from;
    if (Utility.stringIsNullOrEmpty(email)) {
      from = new InternetAddress(props.getProperty("defaultReplyAddress"));
    } else {
      from = new InternetAddress(email);
    }
    InternetAddress to = new InternetAddress(email);
    simpleMessage.setFrom(from);
    simpleMessage.setRecipient(Message.RecipientType.TO, to);
    simpleMessage.setSubject("fgsms SLA Processor Test Message");
    simpleMessage.setContent("This is a test indicating that your fgsms SLA Processor is configured correctly." + "<br>"
        + "<a href=\"" + props.getProperty("fgsms.GUI.URL") + "\">fgsms</a>", "text/html; charset=ISO-8859-1");
    Transport.send(simpleMessage);
    return true;
  } catch (Exception ex) {
    log.log(Level.ERROR, "Error sending SLA alert email! " + ex.getLocalizedMessage());
  }
  return false;
}
private static String allitems = "All-Methods";

代码示例来源:origin: dee1024/housedb

@Override
public void send(String subject, String content){
  // 设置环境信息
  Session session = Session.getInstance(getPro());
  // 创建邮件对象
  Message msg = new MimeMessage(session);
  Transport transport = null;
  try {
    msg.setSubject(subject);
    // 设置邮件内容
    msg.setContent(content,"text/html;charset=utf8");
    // 设置发件人
    msg.setFrom(new InternetAddress(mailAccount));    //注意:此处设置SMTP发件人
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(targetMail));
    transport = session.getTransport();
    // 连接邮件服务器
    transport.connect(mailAccount, mailPassword);
    // 发送邮件
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
  } catch (MessagingException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: org.astrogrid/astrogrid-cea-server

public void run() {
  final Map args = new HashMap();
  try {
  for (Iterator i = inputParameterAdapters(); i.hasNext();) {
    ParameterAdapter a = (ParameterAdapter)i.next();
    args.put(a.getWrappedParameter().getId(),a.process());
  }                        
  setStatus(Status.RUNNING);
  // send the message here.
  Session session = getSessionFromContext();
  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress("sendmail@builtin.astrogrid.org"));
  message.setSubject(args.get(SUBJECT).toString());
  message.setContent(args.get(MESSAGE).toString(),"text/plain");
  message.setRecipient(Message.RecipientType.TO,new InternetAddress(args.get(TO).toString()));
  Transport.send(message);
  setStatus(Status.COMPLETED);
  reportMessage("message sent");
  } catch (NamingException e) {
    reportError("Could not find mail session in JNDI",e);
  } catch (MessagingException e) {
    reportError("Failed to construct email message",e);
  } catch (Throwable t) {
    reportError("Something went wrong",t);
  }
}

代码示例来源:origin: keeps/roda

public void sendMail(String recipient, String message) throws MessagingException {
 if ("".equals(from)) {
  throw new MessagingException();
 }
 Session session = Session.getDefaultInstance(props, authenticator);
 session.setDebug(false);
 Message msg = new MimeMessage(session);
 InternetAddress addressFrom = new InternetAddress(from);
 msg.setFrom(addressFrom);
 msg.addHeader("name", fromActor);
 msg.setSubject(subject);
 InternetAddress recipientAddress = new InternetAddress(recipient);
 msg.setRecipient(Message.RecipientType.TO, recipientAddress);
 String htmlMessage = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%s", message);
 MimeMultipart mimeMultipart = new MimeMultipart();
 MimeBodyPart mimeBodyPart = new MimeBodyPart();
 mimeBodyPart.setContent(htmlMessage, "text/html;charset=UTF-8");
 mimeMultipart.addBodyPart(mimeBodyPart);
 msg.setContent(mimeMultipart);
 // sending the message
 SMTPTransport transport = (SMTPTransport) session.getTransport(this.protocol);
 transport.connect();
 transport.sendMessage(msg, msg.getAllRecipients());
 transport.close();
}

相关文章

微信公众号

最新文章

更多