Friday, May 11, 2012
Send Mail with Java
This is a sample code for writing with java to send email:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class test {
public static void main(String args[]) throws Exception {
String user = "test";
String password = "test";
String fromAddress = "test@localhost";
String toAddress = "test@gmail.com";
Properties properties = new Properties();
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.username", user);
properties.put("mail.smtp.password", password);
properties.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(properties, null);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject("Email from our JAMEs");
message.setText("hiiiiii!!");
Transport.send(message);
System.out.println("Email sent");
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}
Solution:
If we test it and it cannot send. This is the guide for resolving problem as below:
1.Go to the path : \apps\james\conf
2.Open for edit the filename 'james-fetchmail.xml'
3.Change "fetchmail" from false to true as:
4.Restart the Apache James Server.
5.You would see the following messages printed on the console after you start the server:
James Mail Server 2.3.2 Remote Manager Service started plain:4555 POP3 Service started
plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Started.
Sample Class for Get IP Address with VB.NET
Imports System.Management
Imports System.Net.NetworkInformation
'---------------------------------------------------------------------------------------
'File Name: clsIP.vb
'File Desc: Class for getting ip from client
'---------------------------------------------------------------------------------------
' Date Developer Updated Description
'---------------------------------------------------------------------------------------
' August-26-2011 Sout Saret New
'---------------------------------------------------------------------------------------
Public Class clsIP
'-----------------------------------------------------------------------------------
'Method Name : PF_GetIP
'Method Desc : Get the ip from client
'Parameter : N/A
'Return Type : String
'-----------------------------------------------------------------------------------
' Date Developer Updated Description
'-----------------------------------------------------------------------------------
' August-26-2011 Sout Saret New
'-----------------------------------------------------------------------------------
Public Shared Function PF_GetIP() As String
Dim m_MyHost As String = System.Net.Dns.GetHostName
Dim myIPs As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(m_MyHost)
For Each myIP As System.Net.IPAddress In myIPs.AddressList
Return myIP.ToString
Next
If myIPs.ToString Then
Return myIPs.ToString
End If
Return Nothing
End Function
'-----------------------------------------------------------------------------------
'Method Name : PF_GetHostName
'Method Desc : Get the name from client
'Parameter : N/A
'Return Type : String
'-----------------------------------------------------------------------------------
' Date Developer Updated Description
'-----------------------------------------------------------------------------------
' August-26-2011 Sout Saret New
'-----------------------------------------------------------------------------------
Public Shared Function PF_GetHostName() As String
Return System.Environment.MachineName
End Function
'-----------------------------------------------------------------------------------
'Method Name : PF_GetMacAddress
'Method Desc : Get the macaddress from client
'Parameter : N/A
'Return Type : String
'-----------------------------------------------------------------------------------
' Date Developer Updated Description
'-----------------------------------------------------------------------------------
' August-26-2011 Sout Saret New
'-----------------------------------------------------------------------------------
Public Shared Function PF_GetMacAddress() As String
Dim M_networkcard() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
Dim M_netCard As String = M_networkcard(0).GetPhysicalAddress.ToString()
Return M_netCard
End Function
End Class
Subscribe to:
Comments (Atom)