Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how add bcc to java mail

Status
Not open for further replies.

fuadhamidov

Programmer
Sep 22, 2003
98
TR
hi

i want to sent bcc mail by java mail. i have found some sample code from internet. naerly all of them are same. of course i can add any e-mail address to 'To' but i want to add to 'Bcc'.

is there any way?

thanks for any help
 
Code:
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
 
I've done this before. Try this.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;


public class EMail
{
protected String getMailHost()
{
return Config.getConfig().getMailHost();
}

private String text;
/** Gets message text */
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}

private String subject;
/** Gets message subject */
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}

private String from="";
public void setFrom(String from)
{
this.from = StringUtil.null2empty(from);
}
public String getFrom()
{
return from;
}

private String cc="";
public void setCC(String cc)
{
this.cc = StringUtil.null2empty(cc);
}
public String getCC()
{
return cc;
}

private List addresses;
private List ccAddresses;
protected List getRecipients()
{
if (addresses==null)
{
addresses = new ArrayList();
}
return addresses;
}

public void setRecipients(String[] addresses)
{
this.addresses = Arrays.asList(addresses);
}
public void addRecipient(String emailAddress)
{
getRecipients().add(emailAddress);
}

/** Sends email messages with supplied text to all recipients */
public void send() throws MessagingException
{
Properties props = new Properties();
props.put("mail.smtp.host",getMailHost());
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(getFrom()));
message.setText(getText());
message.setSubject(getSubject());

Iterator i = getRecipients().iterator();
while (i.hasNext())
{
String addressStr = (String) i.next();
Address address = new InternetAddress(addressStr);
message.addRecipient(Message.RecipientType.TO,address);

}
/** Tokenizes the String cc and sort of iterates through it.
default delimiter is a space */
StringTokenizer st = new StringTokenizer(cc);
while (st.hasMoreTokens())
{
String addressStr = (String) st.nextToken();
Address address = new InternetAddress(addressStr);
message.addRecipient(Message.RecipientType.CC,address);

}

Transport.send(message);
//debug statements
System.out.println("Would have sent emails to:"+getRecipients().
toString());
System.out.println("/n mail host is: " + getMailHost());
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top