AGMedlem sedan mars 20001 266 inlägg Skall fixa iordning ett anmälningsformulär nu. Skall bygga på prinxcipen att man får ett mail, med adress för att aktivera loginet. Adressen blir ju nåt iform med att verifiera mailadress, id, typ. Kruxet är hur jag gör för att använda javamail. För att skicka ut ett automatiskt mail, nån exempelkod, även tips på hur jag skulle kunna effektivisera denna verifikation tas tacksamt emot.
Andreas
ViktorMedlem sedan aug. 20021 752 inlägg Lite sent kanske men här kommer ett svar och kod exempel
package com.vgsoftware.util.internet.mail;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.io.IOException;
import java.util.Properties;
/**
* A simple class that sends a email
*
* @author Viktor Gars
* @version 1.0
*/
public class Mail
{
private String host="";
/**
* Default constructor
*/
public Mail()
{
}
/**
* Constructor that takes the mail host that will be used when sending mails as a parameter
*
* @param host the SMTP mail server that will be used for sending the email.
*/
public Mail(String host)
{
this.host=host;
}
/**
* Send a email
*
* @param to The email address to the reciver.
* @param from The sendars email address.
* @param subject The subject of the email.
* @param text The body text of the email.
*
* @return true if successfull, false if somthing went wrong
*/
public boolean sendMail(String to, String from, String subject, String text)
{
return(sendMail(host, to, from, subject, text));
}
/**
* Send a email
*
* @param host the SMTP mail server that will be used for sending the email.
* @param to The email address to the reciver.
* @param from The sendars email address.
* @param subject The subject of the email.
* @param text The body text of the email.
*
* @return true if successfull, false if somthing went wrong
*/
public boolean sendMail(String host, String to, String from, String subject, String text)
{
try
{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(text);
// Send message
Transport.send(message);
}
catch(AddressException e)
{
e.printStackTrace(System.err);
return(false);
}
catch(MessagingException e)
{
e.printStackTrace(System.err);
return(false);
}
return(true);
}
}
/Viktor