on 11-21-2007 9:19 AM
Hi All,
I have created an application in webdynpro for uploading files on the server.
Now i want that on action of uploading the file when the file is uploaded a mail should be sent to a particular group of users. the user to which email should be sent will be defined early like we dont have to enter email address on the runtime, it should send email automatically on the click of button.
The email should be sent to the users available in the portal so please tell me how to extract the email id on the basis of ep user id as we extract user first name and last name etc.
thanks and regards...
Gaurav Makin
Hi,
from the IUser object use the method String getEmail()
Gets the user's default email address.
http://help.sap.com/javadocs/NW04/current/um/com/sap/security/api/IUser.html
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,
As their is no difference in sending mail from WD or any other app
use the following sample code
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public void send Mail {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = // To user id either from the context or hard coded
String from = // From user id either from the context or hard coded
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "smtp.yourisp.net";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
// Instantiatee a message
Message msg = new MimeMessage(session);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through WD Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through WD Java.\n" +
"Here is line 2.");
//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
Regards
Ayyapparaj
hi Ayyappa,
Thanks for ur reply
i tried ur code but there is a problem when i type
Message msg = new MimeMessage(session);
Its giving error of Type mismatch cannot convert from MimeMessage to Message.
And if i use
MimeMessage msg = new MimeMessage(session);
then the error is in
msg.setRecipients(Message.RecipientType.TO ,address);
it says cannot resolve Message.RecipientType.TO
And Transport.send(msg) says the method send(MimeMessage ) is undefind for the type Tranport.
Please help
Thanks
thanks akshaya,
Its not showing any error now but the code is not working also.
I think there is some problem with Isp/s mail server and smtp host.
I am working on the servers in US so which isp host i have to give local or US..
Ansl also tell me what should be replaced with "mail.smtp.host". and isp mail server is the Exchange server address or something else.
thanks
Gaurav
check this code(this is sending a file as an attachment using mail,u can change it accordingly to ur need)
try
{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
<b> props.put("mail.smtp.host", "name of the exchange server or ip server where exchange server is residing");</b>
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
// Get session
Authenticator loAuthenticator = new MailAuthenticator();
Session session = Session.getInstance(props, loAuthenticator);
String mailfrom = "from address";
String mailto = "to address";
String ccmailid = "cc address";
String subject = "Subject";
System.setProperty("mail.mime.encodeeol.strict", "true");
MimeMessage mimemessage = new MimeMessage(session);
// set FROM
mimemessage.setFrom(new InternetAddress(mailfrom));
// set DATE
mimemessage.setSentDate(new java.util.Date());
// set SUBJECT
mimemessage.setSubject(subject);
// set TO address
try
{
mimemessage.setRecipients(
javax.mail.Message.RecipientType.TO,
mailto);
}
catch (Exception exception1)
{
System.out.println(
"\tError in setting recipients ......\t"
+ exception1.getMessage());
}
// set message BODY
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setText("Answers of ");
// attach message BODY
MimeMultipart mimemultipart = new MimeMultipart();
mimemultipart.addBodyPart(mimebodypart);
// attach FILE
mimebodypart = new MimeBodyPart();
try
{
FileDataSource filedatasource = new FileDataSource(filepath);
mimebodypart.setDataHandler(new DataHandler(filedatasource));
}
catch (Exception exception3)
{
System.out.println(
"\tError in sending file not been able to attach ......\t"
+ exception3.getMessage());
}
mimebodypart.setFileName(userName+"_Answers.pdf"); // set FILENAME
mimemultipart.addBodyPart(mimebodypart);
mimemessage.setContent(mimemultipart);
String strResult = "";
//set CC MAIL and SEND the mail
if (!mailto.equals(""))
{
// set CC MAIL
try
{
// send MAIL
Transport.send(mimemessage);
System.out.println("\tSent Successfully..........");
strResult = "\tSent Successfully..........";
}
catch (Exception exception4)
{
System.out.println(
"\tError in sending Address Try........."
+ exception4.getMessage());
}
}
else
{
System.out.println("\tMail operation Failed..........\t");
strResult = "\tMail operation Failed..........\t";
}
}
catch (Exception ex)
{
System.out.println("ddd" + ex.getMessage());
}
MailAuthenticator.java
/*
Created on Oct 31, 2007
*
To change the template for this generated file go to
Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.psft;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MailAuthenticator extends Authenticator {
public PasswordAuthentication
getPasswordAuthentication() {
String username, password;
username = "username";
password = "password";
return new PasswordAuthentication(
username, password);
}
}
hope this helps
Message was edited by:
Armin Reichert
hi Bala,
I have used your code as follows for sending mail without attachment:-
try
{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", "IP of the exchange server");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
// Get session
Authenticator loAuthenticator = new MailAuthenticator();
Session session = Session.getInstance(props, loAuthenticator);
String mailfrom = "Email id here";
String mailto = "to address";
String ccmailid = "cc address";
String subject = "Subject";
System.setProperty("mail.mime.encodeeol.strict", "true");
MimeMessage mimemessage = new MimeMessage(session);
// set FROM
mimemessage.setFrom(new InternetAddress(mailfrom));
// set DATE
mimemessage.setSentDate(new java.util.Date());
// set SUBJECT
mimemessage.setSubject(subject);
// set TO address
try
{
mimemessage.setRecipients(
javax.mail.Message.RecipientType.TO,
mailto);
}
catch (Exception exception1)
{
System.out.println(
"\tError in setting recipients ......\t"
+ exception1.getMessage());
}
// set message BODY
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setText("Answers of ");
// attach message BODY
MimeMultipart mimemultipart = new MimeMultipart();
mimemultipart.addBodyPart(mimebodypart);
// attach FILE
mimebodypart = new MimeBodyPart();
if (!mailto.equals(""))
{
// set CC MAIL
try
{
// send MAIL
Transport.send(mimemessage);
System.out.println("\tSent Successfully..........");
strResult = "\tSent Successfully..........";
}
catch (Exception exception4)
{
System.out.println(
"\tError in sending Address Try........."
+ exception4.getMessage());
}
}
else
{
System.out.println("\tMail operation Failed..........\t");
strResult = "\tMail operation Failed..........\t";
}
}
catch (Exception ex)
{
System.out.println("ddd" + ex.getMessage());
}
MailAuthenticator.java
/*
Created on Oct 31, 2007
*
To change the template for this generated file go to
Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MailAuthenticator extends Authenticator {
public PasswordAuthentication
getPasswordAuthentication() {
String username, password;
username = "gaurav.makin";
password = "password";
return new PasswordAuthentication(
username, password);
}
but it is not doing anything on the click of button not showing any errors and not sending mail
pls help.
thanks
Gaurav
Message was edited by:
Gaurav Makin
Message was edited by:
Gaurav Makin
Hi,
Chk this http://lifehacker.com/software/email-apps/how-to-use-gmail-as-your-smtp-server-111166.php
Regards
Ayyapparaj
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.