• Post Reply Bookmark Topic Watch Topic
  • New Topic

mail send with image problem  RSS feed

vikassheel gupta
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sending an email the mail is sent successfully as a sting .In string i write the whole HTML code and that works fine but one thing here to be noted down is i have to send a image and that image is in data base as in blob data and a java file reterieve that image and print it in socket .
i am using the struts for mapping to that path
so when ever i try to write
String strItem +="<img src=request.getContextPath()/show.do?id=1234>" >;
when i do in this way then my the control doesnt go to that file instead of that simple string in which html text and a blank image is sent
so tell me the solution
so that i can send image and text through my mail
Here is my mail code:-- Bold text is to map the struts-config file and write the image to the socket

* Created on Aug 17, 2005
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
package com.gpi.h2.web.ecards;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Properties;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import com.gpi.h2.transferobject.CatalougeItem;
import com.gpi.h2.transferobject.EcardsInfo;
import com.gpi.h2.transferobject.UserInfo;
import com.gpi.h2.businessdelegate.ecards.EcardsListImpl;
import com.gpi.h2.web.ecards.SendEcardForm;

* @author Administrator
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
public class SendEcardMailAction extends Action

private static final String SMTP_HOST_NAME = "";
private static final String SMTP_AUTH_USER = "tarund";
private static final String SMTP_AUTH_PWD = "password";
private String emailMsgTxt = "";
public String emailSubjectTxt = "";
public String emailFromAddress = "";
// private String filename="c:/two.jpg";
ServletOutputStream out= null;

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception
String userid=((UserInfo)request.getSession().getAttribute("UserInfo")).getM_strUserId();

boolean success=false;
HttpSession session = request.getSession(false);
String strItem=null;
String strItemId=null;
String strRecipients=null;
boolean save_image=false;

SendEcardForm ecardform= (SendEcardForm)form;
CatalougeItem l_CatalougeItem= new CatalougeItem();
EcardsListImpl l_EcardListImpl= new EcardsListImpl();
EcardsInfo l_EcardInfo= new EcardsInfo();

String itemid=request.getParameter("id");

String[] astrRecipients;

String forsave=ecardform.getForSave();

//*****************************************HTML TEXT**************************************

strItem ="<html><body>";
strItem +="<table border=1 align='center' bgcolor='"+ecardform.getCard_bgcolor()+"'>";
strItem +="<tr><td align=left >";
strItem +="<font face="+ecardform.getCard_fontface()+" size="+ecardform.getCard_fontSize()+" color="+ecardform.getCard_fontColor()+">";
strItem +=""+ecardform.getCard_title()+" ";
strItem +="</font>";
strItem +="</tr></td>";
strItem +="<tr><td>";
strItem +="<img src=\http://localhost:8080/Harmonization/ShowEcard.do?id="+itemid+" >";
strItem +="</tr></td>";
strItem +="<tr><td align='center'>";
strItem +="<font face="+ecardform.getCard_fontface()+" size="+ecardform.getCard_fontSize()+" color="+ecardform.getCard_fontColor()+">";
strItem +=" "+ecardform.getMessage()+" ";
strItem +="</font>";
strItem +="</tr></td>";
strItem +="<tr><td align='right'>";
strItem +="<font face="+ecardform.getCard_fontface()+" size="+ecardform.getCard_fontSize()+" color="+ecardform.getCard_fontColor()+">";
strItem +=" "+ecardform.getSignature()+" ";
strItem +="</font>";
strItem +="</tr></td>";
strItem +="</table>";
strItem +="</body></html>";

//************************************************* End of HTML TEXT*****************


if (strRecipients!=null && strRecipients.length()>0)
SendEcardMailAction smtpMailSender=new SendEcardMailAction();
smtpMailSender.postMailTo( astrRecipients, emailSubjectTxt, emailMsgTxt, emailFromAddress);

}catch(Exception e)
request.setAttribute("message"," Sorry ..Your mail has not been sent to your friend(s)");
return mapping.findForward("error");

request.setAttribute("message","You have successfully mail the item to your friend(s)");

return mapping.findForward("ecards.confirmation");

public void postMailTo( 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.getInstance(props, auth);


// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);

//InternetAddress addressTo= new InternetAddress(recipient);
Address[] addressTo=new Address[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

//********************** code for attachment************************

MimeMultipart mp= new MimeMultipart();

MimeBodyPart attach = new MimeBodyPart();

attach = new MimeBodyPart();


//**********************end of attachment code***************************

//msg.setContent(message, "text/html");
Transport transport = session.getTransport("smtp");
transport.connect( "", SMTP_AUTH_USER, SMTP_AUTH_PWD);

transport.sendMessage(msg, msg.getRecipients( Message.RecipientType.TO));


* 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);

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!