Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Help me to convert this code in C# into a Java code

Help me to convert this code in C# into a Java code

Scheduled Pinned Locked Moved Java
csharpjavahelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hellboy_83
    wrote on last edited by
    #1

    public class Program
    {
    public static void Main(string[] args)
    {
    try
    {
    // Check arguments
    if (args.Length != 3)
    {
    ShowUsage();
    return;
    }

                // Create message
                MailMessage mmMessage = new MailMessage();
                mmMessage.To.Add(new MailAddress(args\[2\] as string));
                mmMessage.From = new MailAddress(args\[1\] as string);
                mmMessage.Subject = "TestMail";
                mmMessage.Body = "This is a test";
    
                // Send message
                SmtpClient sc = new SmtpClient(args\[0\] as string);
                sc.Send(mmMessage);
    
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                //Pause
                Console.WriteLine();
                Console.Write("Press a key >");
                Console.ReadKey();
            }
        }
    
        private static void ShowUsage()
        {
            Console.WriteLine("Usage");
            Console.WriteLine("TestMail \[SmtpServer\] \[FromAddress\] \[ToAddress\]");
        }
    
    } // end Program
    

    Regards,

    D 1 Reply Last reply
    0
    • H hellboy_83

      public class Program
      {
      public static void Main(string[] args)
      {
      try
      {
      // Check arguments
      if (args.Length != 3)
      {
      ShowUsage();
      return;
      }

                  // Create message
                  MailMessage mmMessage = new MailMessage();
                  mmMessage.To.Add(new MailAddress(args\[2\] as string));
                  mmMessage.From = new MailAddress(args\[1\] as string);
                  mmMessage.Subject = "TestMail";
                  mmMessage.Body = "This is a test";
      
                  // Send message
                  SmtpClient sc = new SmtpClient(args\[0\] as string);
                  sc.Send(mmMessage);
      
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex);
              }
              finally
              {
                  //Pause
                  Console.WriteLine();
                  Console.Write("Press a key >");
                  Console.ReadKey();
              }
          }
      
          private static void ShowUsage()
          {
              Console.WriteLine("Usage");
              Console.WriteLine("TestMail \[SmtpServer\] \[FromAddress\] \[ToAddress\]");
          }
      
      } // end Program
      

      Regards,

      D Offline
      D Offline
      Dave Doknjas
      wrote on last edited by
      #2

      I don't have a conversion of the 'MailMessage' or 'MailAddress' types, or 'Console.ReadKey', but I have a conversion that should help a bit:

      public class Program
      {
      	public static void main(String\[\] args)
      	{
      		try
      		{
      			// Check arguments
      			if (args.length != 3)
      			{
      				ShowUsage();
      				return;
      			}
      			// Create message
      			MailMessage mmMessage = new MailMessage();
      			mmMessage.To.add(new MailAddress((String)((args\[2\] instanceof String) ? args\[2\] : null)));
      			mmMessage.From = new MailAddress((String)((args\[1\] instanceof String) ? args\[1\] : null));
      			mmMessage.Subject = "TestMail";
      			mmMessage.Body = "This is a test";
      			// Send message
      			SmtpClient sc = new SmtpClient((String)((args\[0\] instanceof String) ? args\[0\] : null));
      			sc.Send(mmMessage);
      		}
      		catch (Throwable ex)
      		{
      			System.out.println(ex);
      		}
      		finally
      		{
      			//Pause
      			System.out.println();
      			System.out.print("Press a key >");
      			Console.ReadKey();
      		}
      	}
      	private static void ShowUsage()
      	{
      		System.out.println("Usage");
      		System.out.println("TestMail \[SmtpServer\] \[FromAddress\] \[ToAddress\]");
      	}
      } // end Program
      

      David Anton http://www.tangiblesoftwaresolutions.com Convert VB to C#, C++, or Java Convert C# to VB, C++, or Java Convert C++ to C#, VB, or Java Convert Java to C#, C++, or VB

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups