Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

mail send with image problem

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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 = "192.168.0.250";
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
{
try
{
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");
l_EcardInfo.setCard_bgcolor(ecardform.getCard_bgcolor());
l_EcardInfo.setFilename(ecardform.getFilename());
l_EcardInfo.setCard_fontColor(ecardform.getCard_fontColor());
l_EcardInfo.setCard_fontSize(ecardform.getCard_fontSize());
l_EcardInfo.setCard_fontface(ecardform.getCard_fontface());
l_EcardInfo.setUserID(ecardform.getUserID());
l_EcardInfo.setCatagoryid(ecardform.getCatagoryid());
l_EcardInfo.setUserID(userid);


String[] astrRecipients;

String forsave=ecardform.getForSave();
if(forsave!=null)
{
save_image=l_EcardListImpl.saveECardLayout(l_EcardInfo);
}
strRecipients=ecardform.getRec_email();
astrRecipients=strRecipients.split(",");



//strItem=filename;
emailSubjectTxt=ecardform.getSubject();
//*****************************************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*****************



emailMsgTxt=strItem;
System.out.println(emailMsgTxt);
emailFromAddress=ecardform.getSen_email();

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

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

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

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();
System.out.println(auth);
Session session = Session.getInstance(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(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
msg.setSubject(subject);

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

MimeMultipart mp= new MimeMultipart();

MimeBodyPart attach = new MimeBodyPart();

attach = new MimeBodyPart();
attach.setContent(message,"text/html");
mp.addBodyPart(attach);


msg.setContent(mp);

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

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

transport.sendMessage(msg, msg.getRecipients( Message.RecipientType.TO));
//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;
System.out.println("PasswordAuthentication");
return new PasswordAuthentication(username, password);
}
}

}
 
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question in multiple forums.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!