on 10-01-2007 8:49 AM
Hi all,
I have the following problem/task:
In my application I generate the body for an hmtl-mail. Now I want to send this mail to different recipients, distribution lists, etc.
Does anyone has an example-coding?
I know that there is a lot of coding posted in several threads, but up to know I can't find/develop a working solution!
I tried the following coding, but I got the error:
Sending failed; nested exception is: javax.mail.SendFailedException: Invalid Addresses; nested exception is: javax.mail.SendFailedException: 550 5.7.1 Unable to relay for dominik.herz@sap.com
Properties props = new Properties();
String host = "dewdfe10.wdf.sap.corp";
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
Address toAddress = new InternetAddress();
Address ccAddress = new InternetAddress();
Address bccAddress = new InternetAddress();
Address fromAddress = new InternetAddress();
try
{
MimeMultipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
/* the following statements build up the email */
fromAddress = new InternetAddress("test@sap.com");
message.setFrom(fromAddress);
//toAddress = new InternetAddress(wdContext.currentContextElement().getCurrentUserId()+"@sap.com");
toAddress = new InternetAddress("dominik.herz@sap.com");
message.setRecipient(Message.RecipientType.TO,toAddress);
message.setSubject(eMailContent.getSubject());
messageBodyPart.setText("Test");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
}
//exceptions part
catch (AddressException e)
{
wdComponentAPI.getMessageManager().reportWarning(e.
getLocalizedMessage());
e.printStackTrace();
}
catch (SendFailedException e)
{
wdComponentAPI.getMessageManager().reportWarning(e.
getLocalizedMessage());
e.printStackTrace();
}
catch (MessagingException e)
{
wdComponentAPI.getMessageManager().reportWarning(
e.getLocalizedMessage());
e.printStackTrace();
}
Thanks and best regards,
Dominik
You just need to put the correct SMTP server of your company.
If you want mails to be send out to different people you can create a class (for eg: MailSender.java) and have method sendMail which accepts FromAdress, ToAddress,Subject). Add the class file to your WD project.
Now call this method from the WD application as follows:-
String strFromAddr = <FROM ADDRESS>
String strToAddr = <TO ADDR>
String StrSubject = <SUBJECT>
MailSender mailSend = new MailSender();
mailSend.sendMail(strFromAddr,strToAddr,strSubject);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
This Program gives any example on how to do SMTP Authentication
(User and Password verification)
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
/* To use this program, change values for the following three constants,
SMTP_HOST_NAME -- Has your SMTP Host Name
SMTP_AUTH_USER -- Has your SMTP Authentication UserName
SMTP_AUTH_PWD -- Has your SMTP Authentication Password
Next change values for fields
emailMsgTxt -- Message Text for the Email
emailSubjectTxt -- Subject for email
emailFromAddress -- Email Address whose name will appears as "from" address
Next change value for "emailList".
This String array has List of all Email Addresses to Email Email needs to be sent to.
Next to run the program, execute it as follows,
SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();
*/
public class SendMailUsingAuthentication
{
private static final String SMTP_HOST_NAME = "myserver.smtphost.com";
private static final String SMTP_AUTH_USER = "myusername";
private static final String SMTP_AUTH_PWD = "mypwd";
private static final String emailMsgTxt = "Online Order Confirmation Message. Also include the Tracking Number.";
private static final String emailSubjectTxt = "Order Confirmation Subject";
private static final String emailFromAddress = "me@myserver.com";
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = {"mark@yahoo.com", "robin@apple.com"};
public static void main(String args[]) throws Exception
{
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail( String recipients[ ], String subject,
String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo<i> = new InternetAddress(recipients<i>);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
/**
SimpleAuthenticator is used to do simple authentication
when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
Regards
Ayyapparaj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ,
1) Please read the following article on relaying denied: http://www.hiteksoftware.com/knowledge/articles/026.htm
Most likely the issue is that your company is using an Exchange mail server which has to allow your account and/or IP address to send smtp email. You would have to contact your mail server admin for this.
Regards
Ayyapparaj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.